php 三元运算符给出错误

标签 php if-statement ternary-operator

在 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/

相关文章:

php - 如何在owncloud后端服务器上安装新模块

java - 由于我的 if 语句导致编译错误

actionscript-3 - ActionScript 三元运算符

swift - 如何修复 '? :' 表达式中的结果值具有不匹配的类型 'String' 和 '[Any]?'

php - 如何编写mysql查询来跟踪今天的记录

c# - PHP 相当于 C# 中的 uniqid()

php - 将图像上传到文件夹中

javascript - 我这里哪里出错了?

swift - 一旦答案明确,Swift 中是否有一个运算符会停止对多表达式条件语句的求值?

cmake - CMake 生成器表达式中的三元运算符