加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php三元运算符给出错误

发布时间:2020-12-13 17:45:14 所属栏目:PHP教程 来源:网络整理
导读:在 PHP中,如果我有这样的三元组: $my_thing = $this-myAttribute ? $this-myAttribute : "No attribute was set."; 它可以缩写为这样吗? $my_thing = $this-myAttribute ?: "No attribute was set." 我以为我记得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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读