Arrays and Null Coalescing. A simple explanation of how they work together

The null coalescing operator(??) can be used in PHP for accessing array elements and assigning default values when a key does not exist or its value is null.

Using ?? is a safe way to work with arrays since it does not modify the original array; it only returns a value.

The null coalescing operator helps write cleaner and safer code for arrays avoiding verbose isset() checks.

Null checks with isset method.
(click on the image to open in a new tab)

The above code can be written like:

With Null Coalescing operator null checks are less verbose.
(click on the image to open in a new tab)

A much less verbose alternative for sure.

Accessing Array Elements with Default Values

We can use the null coalescing operator to check if an array has a key and assign a default one if it doesn’t.

Let’s see some examples:

In the previous example we looked at an array of recipes. We can access a recipe with bracket notation provided that it has been declared and initiated. We set a message as a default value if the recipe is not in the array.

Here is what we get in the former case:

Example of accessing array elements with default values
(click on the image to open in a new tab)

And now what we get in the later:

If an element is not in the array a default message is displayed.
(click on the image to open in a new tab)

Had we not used the null coalescing operator, the result in the latter case would be a Warning.

Without Null Coalescing operator we would get a Warning!
(click on the image to open in a new tab)

Updating Array Values

We can use the nullish coalescing assignment operator ??=, introduced in PHP 7.4, to update an array with a default value if a key doesn’t exist.

Example of Nullish Coalescing Assignment operator
(click on the image to open in a new tab)

Had we used the null coalescing operator alone, wouldn’t set the ‘phone’ element in the array.

Null Coalescing operators can’t set an element in the array.
(click on the image to open in a new tab)

Our code simply generates a return value

Although Null Coalescing doesn’t set an index in the array, it generates a return value.
(click on the image to open in a new tab)

To actually add a new key-value pair to an array, we must explicitly set the value.

The nullish coalescing operator assignment is a more compact syntax of this assignment.

Chaining Multiple Array Keys

Using this operator, keys are checked sequentially, in the given order.

Null Coalescing operator ignores unset element without warning and returns the first one in the order.
(click on the image to open in a new tab)

Here, country and state are ignored and city is returned.

Order of checking plays a crucial role since the first element checked in the chain is the one that will be returned.

The first checked element in the chain is the one returned.
(click on the image to open in a new tab)

Conclusion

The null coalescing operator is very helpful regarding arrays when we are not sure if an index exists in the array.

Although my blog doesn’t support comments, feel free to reply via email or X.