PHP 中的 ?: 运算符(“Elvis 运算符”)
我今天在一些 PHP 代码中看到了这一点:
$items = $items ?: $this->_handle->result('next', $this->_result, $this);
我不熟悉这里使用的 ?:
运算符。它看起来像一个三元运算符,但省略了谓词是否为 true 时计算的表达式。这是什么意思?
I saw this today in some PHP code:
$items = $items ?: $this->_handle->result('next', $this->_result, $this);
I'm not familiar with the ?:
operator being used here. It looks like a ternary operator, but the expression to evaluate to if the predicate is true has been omitted. What does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果左操作数是 真,否则为正确的操作数。
在伪代码中,
大致解析为
或 ,
区别在于
bar
只会被评估一次。您还可以使用它对
foo
进行“自检”,如您发布的代码示例中所示:这会将
bar
分配给foo
> 如果foo
为 null 或 falsey,否则它将保持foo
不变。更多示例:
顺便说一下,它被称为 Elvis 运算符。
It evaluates to the left operand if the left operand is truthy, and the right operand otherwise.
In pseudocode,
roughly resolves to
or
with the difference that
bar
will only be evaluated once.You can also use this to do a "self-check" of
foo
as demonstrated in the code example you posted:This will assign
bar
tofoo
iffoo
is null or falsey, else it will leavefoo
unchanged.Some more examples:
By the way, it's called the Elvis operator.
请参阅文档:
See the docs:
小心数组。我们必须在
?
之后编写一个检查变量,因为:更新
来自 RFC。在 PHP 7 中,运算符 空合并运算符 将执行此操作,例如:
Be careful with arrays. We must write a checking variable after
?
, because:Updated
From RFC. In PHP 7 the operator Null Coalesce Operator will do it, for example:
Elvis 运算符:
?:
是 Elvis 运算符。这是一个二元运算符,它执行以下操作:将
?:
的左侧值强制转换为布尔值,并检查它是否为true
。如果true
它将返回左侧的表达式,如果 false 它将返回右侧的表达式。示例:
何时使用:
Elvis 运算符基本上是三元运算符特定情况的简写语法,即:
Elvis 运算符将通过以下方式使语法更加简洁:
Elvis operator:
?:
is the Elvis operator. This is a binary operator which does the following:Coerces the value left of
?:
to a boolean and checks if it istrue
. Iftrue
it will return the expression on the left side, if false it will return the expression on the right side.Example:
When to use:
The Elvis operator is basically shorthand syntax for a specific case of the ternary operator which is:
The Elvis operator will make the syntax more consise in the following manner:
另一个重要的考虑因素:Elvis Operator 破坏了 Zend Opcache 标记化过程。我发现这很难!虽然这可能已在更高版本中得到修复,但我可以确认 PHP 5.5.38 中存在此问题(带有内置 Zend Opcache v7.0.6-dev)。
如果您发现某些文件“拒绝”缓存在 Zend Opcache 中,这可能是原因之一...希望这会有所帮助!
Another important consideration: The Elvis Operator breaks the Zend Opcache tokenization process. I found this the hard way! While this may have been fixed in later versions, I can confirm this problem exists in PHP 5.5.38 (with in-built Zend Opcache v7.0.6-dev).
If you find that some of your files 'refuse' to be cached in Zend Opcache, this may be one of the reasons... Hope this helps!
是的,这是 PHP 5.3 中的新功能。如果计算结果为 TRUE,则返回测试表达式的值;如果计算结果为 FALSE,则返回替代值。
Yes, this is new in PHP 5.3. It returns either the value of the test expression if it is evaluated as TRUE, or the alternative value if it is evaluated as FALSE.
我认为目的是条件执行:
仅当 $a 的值解析为 FALSE 时才会执行 func() 中的结果。它可以用作
thre赋值是可选的 $a = $a ?: func() 的缩写,类似于:
i think the purpose is conditional execution:
results in func() will only be executed if $a has a value that would resolve to FALSE. it may be used used as a short for
thre assignment is optional $a = $a ?: func() is like: