php - PHP 7.4 中的空合并赋值 ??= 运算符是什么

标签 php php-7.4

我刚刚观看了有关即将推出的 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/

相关文章:

PHP 获取 .txt 文件并编辑一行

php - 使用 SimpleXML 读取 RSS 提要

php - 不能使用带有返回 void 类型提示的箭头函数

php - 为什么 PHP 严格类型允许错误类型的函数参数?

php - 自 2020-06 新版本以来,Eclipse PHP 垃圾邮件发出新警告

php - 使用 Doxygen 时构建文档

php - 这些 SQL 语句有什么问题?

php - 如何使用 Sonata 列表制作一个弹出窗口来选择一个项目

php - 静默 PHP 7.4.8 服务器请求日志 "Accepted", "Closing"

php - 在 macOS Sierra 上安装 PHP 7.4 而无需 brew?