三元运算符-PHP分析错误:语法错误,意外';:';


ternary operator - PHP Parse error: syntax error, unexpected ':'

很长一段时间以来,我使用了这样的东西:

$foo = $foo ?: $bar; // similar to $foo = $foo ? $foo : $bar;

而且效果很好。

但现在我在一个新的主机上使用PHP版本的5.2.17,当我尝试运行类似的代码时,它显示了一个解析错误:

Parse error: syntax error, unexpected ':' in /../

我该如何解决这个问题?

缩短的三元语法仅在PHP 5.3和更新的中可用

Please note that the ternary operator is a statement, and that it
doesn't evaluate to a variable, but to the result of a statement. 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.
So there is a problem with the return-by-reference for the ?: syntax.
That's way I explicitly wrote what should be returned. 

参考站点https://groups.google.com/forum/#!消息/条令用户/qbAvaKH5uI/DF_2XSxFvG4J

我不能说它有bug,但从url来看https://bugs.php.net/bug.php?id=60169看起来是bug

there is also segfault in (***)?:value notation.
like:
   <?php
     $str = array('test');
     list($a, $b) = is_array($str)?:$str;
and this make *the patch doesn't work* (a memory leak)