php三元运算符给出错误

PHP中,如果我有这样的三元组:

$my_thing = $this->myAttribute ? $this->myAttribute : "No attribute was set.";

它可以缩写为这样吗?

$my_thing = $this->myAttribute ?: "No attribute was set."

我以为我记得PHP在它的三元组中支持这个,但现在我收到了一个错误.

最佳答案
PHP 5.3及更高版本支持它.从PHP.net

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

转载注明原文:php三元运算符给出错误 - 代码日志