Bug #52825 [Opn]: return by reference with ternary operator causes notice "Only variable..."

From: Date: Mon, 13 Sep 2010 04:46:55 +0000
Subject: Bug #52825 [Opn]: return by reference with ternary operator causes notice "Only variable..."
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-154285@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52825&edit=1

 ID:                 52825
 User updated by:    mbroxer at post dot ru
 Reported by:        mbroxer at post dot ru
 Summary:            return by reference with ternary operator causes
                     notice "Only variable..."
 Status:             Open
 Type:               Bug
 Package:            *General Issues
 Operating System:   Windows XP SP3
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

I'm sorry, the expected result and actual result got mixed up when I was
submitting. Expected is actual, and actual is expected.


Previous Comments:
------------------------------------------------------------------------
[2010-09-13 06:45:41] mbroxer at post dot ru

Description:
------------
I don't know if it's a bug, or that's how it should be, but it would be
logical to assume that a ternary operator is equal to an IF block:

return $a ? $b : $c;
// ==
if ($a) return $b; else return $c;

I believe that's how it is in C. However when returning a reference, the
first version produces a notice:
"Notice: Only variable references should be returned by reference"

It's hard to tell if this is normal behaviour or not, but it's
confusing. If this is normal functionality, then I think this should be
in documentation, at least in a form of a comment.

Test script:
---------------
class test {
	private $a = array();
	private $b = array();

	function &getA($key) {
		return $key > 0 ? $this->a[$key] : $this->b[$key]; 
		// the following line gives no notice
		// if ($key > 0) return $this->a[$key]; else return $this->b[$key];
	}
}

$a = new test;
$b = &$a->getA('1');

Expected result:
----------------
Notice: Undefined index: 1 in - on line 9

Notice: Only variable references should be returned by reference in - on
line 9



Actual result:
--------------
Notice: Undefined index: 1 in - on line 9




------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52825&edit=1


Thread (4 messages)

« previous php.bugs (#154285) next »