什么是“Null合并”(??)运算符用于? [英] What is the 'Null coalesce' (??) operator used for?
本文介绍了什么是“Null合并”(??)运算符用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
随着一个新的PHP版本,PHP 7的发布,新功能被介绍。这些新功能中的一个是我不熟悉的操作员。 Null合并运算符
。
With the release of a new PHP version, PHP 7, new features are introduced. among these new features is an operator I am not familiar with. The Null coalesce operator
.
这个运算符是什么,什么是好用例?
What is this operator and what are some good use cases?
推荐答案
您可以使用它来初始化可能为null的变量
You can use it to init a variable that might be null
运算符称为空聚结运算符。如果操作数不为null,则返回
左侧操作数;否则返回
右手操作数。
The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.
源: https://msdn.microsoft.com/nl-nl/library/ms173224.aspx
(不依赖于语言)
使用案例
>
You can write
$rabbits;
$rabbits = count($somearray);
if ($rabbits == null) {
$rabbits = 0;
}
您可以使用较短的符号
$rabbits = $rabbits ?? 0;
这篇关于什么是“Null合并”(??)运算符用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文