PHP "??" 合并空值运算符


2020-07-24 14:42:08 by Sikoay with 0 comments 1 Hits
Blog 1

今天在阅读一个组件源码时发现了 ?? 运算符,查找官网后发现时PHP7新加入的,记录一下

官方描述:

In addition to what Lawrence said about assigning a default value, one can now use the Null Coalescing Operator (PHP 7). Hence when we want to assign a default value we can write:

$a = ($fruit ?? 'apple');
//assigns the $fruit variable content to $a if the $fruit variable exists or has a value that is not NULL, or assigns the value 'apple' to $a if the $fruit variable doesn't exists or it contains the NULL value

百度翻译:

除了Lawrence所说的分配默认值之外,现在可以使用Null合并运算符(php7)。因此,当我们想要指定一个默认值时,我们可以写下:

$a=($水果??'苹果“);

//如果$fruit变量存在或其值不为NULL,则将$fruit变量内容分配给$a;如果$fruit变量不存在或包含空值,则将值“apple”分配给$a

Tags:

回复 (0)

Leave a Comment