PHP 速记三元运算符 "?:"意外解析错误 ":"

标签 php syntax ternary-operator php-5.2

我刚刚将一些旧的 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/

相关文章:

javascript - 在 PHP、MySQL 和 JS 站点上存储琐碎数据的有效方法

php - 将php数组数据导出到excel

Python for-else 关系

php - PHP 中 C# 的空值合并运算符 (??)

php - 直接从 PHP 调用 smarty 插件函数

php - 是否可以更改 echo 输出的位置?

c++ - 在 C++ 中,创建一个在调用类的任何其他函数时始终运行的函数

list - 通过函数参数访问 R 列表元素

c# - aspx页面中的三元运算符

c# - 如何在三元运算符中编写多个语句?