gpt4 book ai didi

php 三元运算符给出错误

转载 作者:可可西里 更新时间:2023-10-31 22:12:27 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com