PHP: 请帮忙解释一下这些三元运算符
PHP: Please help explain these ternary operators
谁能解释一下这个运算符?我意识到它现在在 7.4 中已被弃用,因此需要重构。
这个例子:
if ($this->interestAreas()->count() > 0) {
$ids ?: $ids = $this->interestAreas()->pluck('id');
$this->interestAreas()->detach();
}
当使用 ?:
分配变量时,我理解它,例如: $newUser = ($user) :? null;
但不是上面那样。谁能解释一下如何重写?
把?
和:
想象成条件变量,像
这样的字符串
$a = 1 ? $b : $c;
可以这样读:
If $a equals 1 then show $b, if not show $c;
谁能解释一下这个运算符?我意识到它现在在 7.4 中已被弃用,因此需要重构。
这个例子:
if ($this->interestAreas()->count() > 0) {
$ids ?: $ids = $this->interestAreas()->pluck('id');
$this->interestAreas()->detach();
}
当使用 ?:
分配变量时,我理解它,例如: $newUser = ($user) :? null;
但不是上面那样。谁能解释一下如何重写?
把?
和:
想象成条件变量,像
$a = 1 ? $b : $c;
可以这样读:
If $a equals 1 then show $b, if not show $c;