什么是PHP 7.4中的空合并赋值?? =运算符 [英] What is null coalescing assignment ??= operator in PHP 7.4

查看:167
本文介绍了什么是PHP 7.4中的空合并赋值?? =运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看了有关即将推出的PHP 7.4功能的视频,并看到了这个??=新操作符.我已经知道??运算符. 有什么不同?

I've just seen a video about upcoming PHP 7.4 features and saw this ??= new operator. I already know the ?? operator. How's this different?

推荐答案

来自文档:

等于或等于=运算符是赋值运算符.如果left参数为null,则将right参数的值分配给left参数.如果该值不为null,则不执行任何操作.

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';

因此,如果以前从未分配过值,那么基本上只是一种简写方式.

So it's basically just a shorthand to assign a value if it hasn't been assigned before.

这篇关于什么是PHP 7.4中的空合并赋值?? =运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆