PHP Null Coalescing Operator

Until recently, I often found myself using conditional logic with isset() in cases like the following:

// Set a variable to hold the value of $_POST['value']
$value = $_POST['value'];

// If $value is defined and not null, do something
if( isset( $value ) ) {
  // Do something.
}

or perhaps using a ternary:

// If $_POST['value'] is defined and not null,
// Set $value equal to $_POST['value'].
// Otherwise, set $value equal to an empty string.
$value = $_POST['value'] ? $_POST['value'] : '';

The above examples work just fine, and if you’re using an older version of PHP, these might be some of your best options. However, users of PHP 7.0 and newer can take advantage of the null coalescing operator, ??.

The null coalescing operator is essentially a syntactic variation on using a ternary operator with isset(). Take the following example:

$value = $_POST['value'] ?? '';

This example is functionally the same as the previous example, saying “if the first value ($_POST['value']) is defined and not null, set $value equal to it. Otherwise, set $value equal to the second value (in this case an empty string)”. This syntax allows us to avoid writing any isset()checks, and also removes the need to write $_POST['value'] twice like we do in the ternary example.

Stacking Null Coalescing Operators

An especially neat (and potentially useful) feature of the null coalescing operator is the ability to stack them on a single line. Looking at the following example:

$price = $_POST['sale_price'] ?? $_POST['regular_price'] ?? 'Price unavailable';

In this example, if $_POST['sale_price'] is set and not null, we’ll set $price to that value. Otherwise, we’ll check to see if $_POST['regular_price'] is set and not null. If so, we’ll set $price equal to that value. Finally, if the first two checks fail, we will set $price equal to the string ‘Price unavailable’. This single line of code is equivalent to the following example using isset() and conditional logic:

if( isset( $_POST['sale_price'] ) ) {
  $price = $_POST['sale_price'];
} elseif( isset ( $_POST['regular_price'] ) ) {
  $price = $_POST['regular_price'];
} else {
  $price = 'Price unavailable';
}

Personally, I like that we can use the null coalescing operator to save ourselves writing multi-line conditionals like the example above.

Null Coalescing Assignment Operator

PHP 7.4 built upon the null coalescing operator by giving us the null coalescing assignment operator. We can use it like this:

$value ??= 'default';

The null coalescing assignment operator assigns the right operand to the left if the left is null or undefined. Using the example above, if $value is null or undefined, we’ll set it equal to the string ‘default’. It’s the equivalent to writing the following:

if( !isset( $value ) ) {
    $value = 'default';
}
the author profile picture

Ryan Neilson is a software developer from Nova Scotia, Canada, where he lives with his wife and their menagerie of pets. He has worked extensively with PHP, WordPress, JavaScript, React, SCSS and CSS. Ryan can be found professionally at News Corp, where he works as a lead software engineer.