Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator not working with reference variables in PHP [closed]

Why isn't this working?

$a = 'FOO';

$foo = $a ? &$a : 'whatever'; // <- error here

echo $foo;

I get a parse error :|

like image 328
Alex Avatar asked Mar 03 '26 03:03

Alex


1 Answers

If you want to assign a reference using a ternary statement, then you need this clumsy workaround:

 list($foo) = $a ? array(&$a) : array('whatever');

However, as said in the other answers, this seldomly saves memory.

like image 195
mario Avatar answered Mar 04 '26 17:03

mario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!