From:             [EMAIL PROTECTED]
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:

PHP Development Mailing List <>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to