Bug #12247: ternary ?: loses references
| From: | nick at macaw dot demon dot co dot uk | Date: | Thu, 19 Jul 2001 11:02:09 +0000 |
| Subject: | Bug #12247: ternary ?: loses references | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-60278@lists.php.net to get a copy of this message | ||
From: nick@macaw.demon.co.uk
Operating system: Solaris
PHP version: 4.0.6
PHP Bug Type: Variables related
Bug description: ternary ?: loses references
This may be a subtlety of the ?: operator that I failed to spot - then
again it may just be a bug.
Using ?: with references loses the reference, as the example below
illustrates. In both cases a reference to the second argument to a
function should be returned, the result modified and the original argument
displayed. The expectation being that it has now changed. When the
reference is returned from and if-then-else statement all is fine. When the
reference is, or isn't?, returned from ?: the result is not as expected.
The output from the code is
[1] [xx]
[1] [2]
function &return_ref(&$arg1, &$arg2, $cond)
{
if ($cond) { return $arg1; } else { return $arg2; }
}
function &return_ref_ternary(&$arg1, &$arg2, $cond)
{
return ($cond ? $arg1 : $arg2);
}
$arg1 = '1'; $arg2 = '2';
$res =& return_ref($arg1, $arg2, false);
$res = 'xx';
echo "[$arg1] [$arg2]\n";
$arg1 = '1'; $arg2 = '2';
$res =& return_ref_ternary($arg1, $arg2, false);
$res = 'xx';
echo "[$arg1] [$arg2]\n";
-- nick
--
Edit bug report at: http://bugs.php.net/?id=12247&edit=1