What is a null coalescing operator in PHP?

Null coalescing operator

Null coalescing is a binary operator in some programming languages that allows us to assign a default value to a variable if it is null or undefined. It is typically denoted by ?? (double question mark).

The null coalescing operator (??) was introduced in PHP 7. In PHP, the null coalescing operator (??) returns its first operand if the first operand exists and it is not null. If the first operand is null, the null coalescing operator returns its second operand.

It is also known as a double question mark operator.

Syntax

To use the null coalescing operator in PHP, simply use the ?? between two expressions or variables. The syntax is as follows:

$someVariable = $firstOperand ?? $secondOperand

Usage of null coalescing ?? operator

In software development, null checking is the process of verifying whether an object or variable has a null value before attempting to perform any operations on it. Null is a special value in programming that represents the absence of a value or the invalidity of a reference. It is essential to prevent runtime errors.

Null checking can be done using conditional statements such as if-else, ternary operator, or null coalescing operator. It is a good practice to perform null checking regularly throughout the code to ensure the program's stability and reliability.

Before the inclusion of null coalescing operator in PHP, null checking was done by using ternary operator or if-else statement was used in conjunction with isset() function.

In the following three examples, if $_GET['user'] is set and not null, $_GET['user'] will be assigned to $user. Otherwise, "Visitor" will be assigned to $user.

Example 1

<?php
//example of null checking using isset and if-else statement
$user = null;
if(isset($_GET['user']) ){
$user = $_GET['user'];
}
else{
$user = "Visitor";
}
echo "Welcome {$user}";

Example 2

Another example that shows null checking using ternary operator and isset function is provided below.

<?php
$user = isset($_GET['user']) ? $_GET['user'] : 'Visitor';
echo "Welcome {$user}";
?>

Example 3

The null coalescing operator to shorten the null checking steps. A code example is given below.

<?php
$user = $_GET['user'] ?? 'Visitor';
echo "Welcome {$user}";
?>

Shorthand Syntax of null coalescing

We can use the null coalescing operator with assignment operator using the shorthand notation.

<?php
$user = null;
$user ??= "Visitor";
echo "Welcome {$user}";
?>

Explanation

Let's dissect the code line by line:

  • Line 2: The $user variable is initialized with null value.

  • Line 3: We'll use the null coalescing shorthand syntax to check if there is a non-null value in $user then assign that non-null value otherwise assign "Visitor" to the $user variable.

  • Line 4: This line echoes the message.

As we can see that the readability is reduced in the code example given below. Hence, it is recommended to use the regular syntax of null coalescing operator.

Chaining multiple null coalescing operators

The null coalescing operator allows us to chain multiple times. In such cases, the operator will scan from left to right a non-null value.

Consider the example given below. Let's dissect it line by line:

<?php
$theme = $_GET['theme'] ?? $_COOKIE['theme'] ?? 'system-default';
echo "Current theme is: {$theme}"
?>

Explanation

  • Line 2: The $theme function will be assigned the value in the following order: if $_GET['theme'] is set and not null, use its value; otherwise check if $_COOKIES['theme'] is set and not null then use its value; otherwise, use a default value "system-default".

  • Line 3: This line echoes the message with current theme name.

The null coalescing operator is a binary operator which was introduced in PHP 7. It allows the programmers to shorten the code for checking variable values using isset function in conjunction with ternary operator or if-else statement. The null coalescing operator returns the value of the first expression or operand if it is set and not null; otherwise, it returns the value of the second expression or operand.

Note: Keep in mind that the null coalescing operator (?? ) is available in PHP 7.0 and later. While using an older version of PHP, we would need to use ternary operators or other conditional statements to achieve similar functionality.

Free Resources