Is this a bug?
function &test(){ $data = 'test'; return (is_null($data) ? $data : $data); if( is_null($data) ) return $data; else return $data;}$s = test();var_dump($s);
This is to describe my intention. the actual application is much more complicated. The function returns a reference. However, I found that if the three-object operator is used for expression, PHP cannot correctly understand return, and
PHP Notice: Only variable references should be returned by reference
If is used, everything is normal.
I don't know if this is a bug, because many people have discovered it before, and I have encountered this problem in both 5.4.11 and 5.5.0alpha4.
Reply to discussion (solution)
Also found in the manual:
Note: the ternary operator is a statement. Therefore, the result is not a variable but a statement. It is important to return a variable through reference. Return $ var = 42? $ A: $ B; will not work, and a warning will be issued for future PHP versions.
Paste
This is not a BUG! It is a correct syntax analysis.
Your function & test () declares that the function returns a reference. Since it is a reference, there must be a carrier to be referenced.
The three-object operation is a structure, and the result must be assigned to real variables.
$ A = is_null ($ data )? $ Data: $ data;
Return $;
No errors
Of course, function test () does not cause errors.
Many early programs have had this problem since php5.3.