很长一段时间我都用过这样的东西:
$foo = $foo ?: $bar; // similar to $foo = $foo ? $foo : $bar;
它运作良好。
但现在我在使用PHP
Version of的新主机上5.2.17
,当我尝试运行类似的代码时,它显示一个解析错误:
Parse error: syntax error, unexpected ':' in /../
我该如何解决这个问题?
很长一段时间我都用过这样的东西:
$foo = $foo ?: $bar; // similar to $foo = $foo ? $foo : $bar;
它运作良好。
但现在我在使用PHP
Version of的新主机上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/#!msg/doctrine-user/qbSAvaKH5uI/DF_2XSxFvG4J
我不能说它有一个bug
,但从网址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)