【发布时间】:2021-02-26 21:56:08
【问题描述】:
谁能解释一下这个运算符?我意识到它现在在 7.4 中已弃用,因此进行了重构。
这个例子:
if ($this->interestAreas()->count() > 0) {
$ids ?: $ids = $this->interestAreas()->pluck('id');
$this->interestAreas()->detach();
}
当?: 用于分配变量时,我明白这一点,例如:$newUser = ($user) :? null;,但不是如上所述。谁能解释一下如何重写?
【问题讨论】:
-
意义相同,只是他们没有在作业中使用结果。
-
这只是
if声明的简写。 -
是什么让你说它已被弃用?
-
谢谢@Barmar - 我在应用程序中遇到类似用途的错误,但也许这种用途实际上是有效的......
Unparenthesizeda ?: b ? c : d` 已弃用`。 -
7.4 不推荐使用不带括号的嵌套三元组。这与 elvis 运算符无关。
标签: php operators elvis-operator