#12247 [Ver]: ternary ?: loses references

From: Date: Sat, 14 Sep 2002 16:57:29 +0000
Subject: #12247 [Ver]: ternary ?: loses references
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-19356@lists.php.net to get a copy of this message
ID: 12247 User updated by: nick@macaw.demon.co.uk Reported By: nick@macaw.demon.co.uk Status: Verified Bug Type: Scripting Engine problem Operating System: Solaris PHP Version: 4.2.1 New Comment: Agreed. Returning a reference to $x + 2 is a nonsense, and accordingly ?: falls into the same category. The case I quoted was really a special one. Making a change from Sniper doing it (sorry mate :), I'm happy for someone to bogossify this one. Previous Comments: ------------------------------------------------------------------------ [2002-09-14 06:03:05] Xuefer@21cn.com i don't think is as a bug, but a feature request "?:" is operator and "($cond ? $arg1 : $arg2)" is actually an expression not variable imagine how can one return expression like function &return_ref() { return $arg1 + 1; } and how about function &return_ref() { return $arg1 ++; } also for operators: ++ -- += -= *= /= ------------------------------------------------------------------------ [2001-07-19 07:02:09] nick@macaw.demon.co.uk 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 this bug report at http://bugs.php.net/?id=12247&edit=1

« previous php.bugs (#19356) next »