Php 简明教程

PHP - Null Coalescing Operator

Null Coalescing 运算符是 PHP 7 引入的许多新功能之一。“coalescing”一词的意思是将许多事物组合成一个。此运算符用于替换与 isset() 函数结合使用时的三元运算。

The Null Coalescing operator is one of the many new features introduced in PHP 7. The word "coalescing" means uniting many things into one. This operator is used to replace the ternary operation in conjunction with the isset() function.

Ternary Operator in PHP

PHP 有一个三元运算符,由“ ? ”符号表示。三元运算符比较布尔表达式,如果为真则执行第一个操作数,否则执行第二个操作数。

PHP has a ternary operator represented by the "?" symbol. The ternary operator compares a Boolean expression and executes the first operand if it is true, otherwise it executes the second operand.

expr ? statement1 : statement2;

Example

让我们使用三元运算符来检查某个变量是否设置,借助 isset() 函数,如果已声明则返回 true,否则返回 false。

Let us use the ternary operator to check if a certain variable is set or not with the help of the isset() function, which returns true if declared and false if not.

<?php
   $x = 1;
   $var = isset($x) ? $x : "not set";
   echo "The value of x is $var";
?>

它将生成以下 output

It will produce the following output

The value of x is 1

现在,让我们删除“x”的声明并重新运行代码:

Now, let’s remove the declaration of "x" and rerun the code −

<?php
   # $x = 1;
   $var = isset($x) ? $x : "not set";
   echo "The value of x is $var";
?>

现在,代码将产生以下 output

The code will now produce the following output

The value of x is not set

The Null Coalescing Operator

Null Coalescing 运算符由“??”符号表示。它用作三元运算符与 isset() 结合使用的便捷快捷方式。它返回其第一个操作数(如果存在且不为 null);否则返回其第二个操作数。

The Null Coalescing Operator is represented by the "??" symbol. It acts as a convenient shortcut to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not null; otherwise it returns its second operand.

$Var = $operand1 ?? $operand2;

第一个操作数检查特定变量是否为 null 或不为 null(或是否已设置或未设置)。如果它不是 null,则返回第一个操作数,否则返回第二个操作数。

The first operand checks whether a certain variable is null or not (or is set or not). If it is not null, the first operand is returned, else the second operand is returned.

Example

请看以下示例:

Take a look at the following example −

<?php
   # $num = 10;
   $val = $num ?? 0;
   echo "The number is $val";
?>

它将生成以下 output

It will produce the following output

The number is 0

现在取消对将 $num 设置为 10 的第一条语句的注释,并重新运行代码:

Now uncomment the first statement that sets $num to 10 and rerun the code −

<?php
   $num = 10;
   $val = $num ?? 0;
   echo "The number is $val";
?>

现在会生成以下 output

It will now produce the following output

The number is 10

空值合并运算符的一个实用应用是在检查用户名是否由客户端浏览器提供时。

A useful application of Null Coalescing operator is while checking whether a username has been provided by the client browser.

Example

下面代码从 URL 中读取 name 变量。如果 URL 中 name 参数确实有值,则会显示一条欢迎信息。但是,如果没有,则用户被称为 Guest。

The following code reads the name variable from the URL. If indeed there is a value for the name parameter in the URL, a Welcome message for him is displayed. However, if not, the user is called Guest.

<?php
   $username = $_GET['name'] ?? 'Guest';
   echo "Welcome $username";
?>

假设此脚本 “hello.php” 位于 PHP 服务器的 htdocs 文件夹中,在 URL 中输入 http://localhost/hello.php?name=Amar ,浏览器将显示以下消息 −

Assuming that this script "hello.php" is in the htdocs folder of the PHP server, enter http://localhost/hello.php?name=Amar in the URL, the browser will show the following message −

Welcome Amar

如果 http://localhost/hello.php 是 URL,则浏览器将显示以下消息 −

If http://localhost/hello.php is the URL, the browser will show the following message −

Welcome Guest

空值合并运算符用作检查 isset() 函数的三元运算符特定案例的替换项。因此,以下语句给出了类似的结果 −

The Null coalescing operator is used as a replacement for the ternary operator’s specific case of checking isset() function. Hence, the following statements give similar results −

<?php
   $username = isset($_GET['name']) ? $_GET['name'] : 'Guest';
   echo "Welcome $username";
?>

现在会生成以下 output

It will now produce the following output

Welcome Guest

你可以连接 “??” 运算符,如下所示 −

You can chain the "??" operators as shown below −

<?php
   $username = $_GET['name'] ?? $_POST['name'] ?? 'Guest';
   echo "Welcome $username";
?>

现在会生成以下 output

It will now produce the following output

Welcome Guest

如果 $name 变量未通过 GET 或 POST 方法设置,这会将 username 设置为 Guest。

This will set the username to Guest if the variable $name is not set either by GET or by POST method.