我刚刚将一些旧的 PHP 文件上传到新服务器,并且在速记三元操作上遇到解析错误(意外的 ':')。例如:
$y = $x ?: "Some default";
php 版本是 5.2.16 代码中到处都是这些简写? p>
最佳答案
这仅在 PHP 5.3 之后可用
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
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.1
见 this example了解更多上下文。
或更有用但在评论中注明: http://www.php.net/manual/en/control-structures.if.php#102060
1 http://php.net/manual/en/language.operators.comparison.php
关于PHP 速记三元运算符 "?:"意外解析错误 ":",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6277222/