I use followed somewhere in my code:
if (isset($flat[$pid])) {
$branch = &$flat[$pid]['items'];
} else {
$branch = &$tree;
}
All ok, but when I want to short it to:
$branch = isset($flat[$pid]) ? &$flat[$pid]['items'] : &$tree;
I get:
syntax error, unexpected '&' ...
What I'm doing wrong?
This is because the ternary operator is an expression, so it doesn't evaluate to a variable. And a quote from the manual:
Note: Please note that the ternary operator is an expression, and that it doesn't evaluate to a variable, but to the result of an expression. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With