在 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 三元运算符给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17330964/