我刚刚观看了有关即将推出的 PHP 7.4 功能的视频,并看到了这个新的 ??=
运算符。我已经知道 ??
运算符。
这有什么不同?
最佳答案
来自docs :
Coalesce equal or ??=operator is an assignment operator. If the left parameter is null, assigns the value of the right paramater to the left one. If the value is not null, nothing is done.
示例:
// The folloving lines are doing the same
$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';
// Instead of repeating variables with long names, the equal coalesce operator is used
$this->request->data['comments']['user_id'] ??= 'value';
所以它基本上只是一个赋值的简写,如果之前没有赋值过的话。
关于php - PHP 7.4 中的空合并赋值 ??= 运算符是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59102708/