null coalescing operator – Code Refactoring School

Code refactoring school is a series of short articles where we cover some of the most common tactics to improve code quality and readability. In this article, we will be discussing “null coalescing operator” which was introduced in PHP 7.0. You can read more on php.net.

The problem

One thing you can see a lot in old codebases is something like:

$company_data = [];
$data = isset($company_data['xdoo']) ? $company_data['xdoo'] : 'default';
// $data has value 'default'

What happens in the second line of code?

We are trying to get specific company data from array $company_data. To avoid getting “Undefined array key” warning/notice, before we access specific array key, we are checking if that array key exists. If the key exists, we save the value in $data variable. Otherwise, we save string default. In our example, variable $data will have string value default since array key xdoo doesn’t exist in $company_data array.

The solution

How can we improve?

We can do that by using “null coalescing operator” – ??. php.net article describes it like:

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing 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.

php.net

What does this mean?

“Syntactic sugar” means the that the code will produce the same result, it will just look differently.

“Needing to use ternary in conjunction with isset()” means that we have a case where we need to use ternary operator condition ? true : false with isset() check in the condition.

Finally, let’s see our optimized code.

$company_data = [];
$data = $company_data['xdoo'] ?? 'default';
// $data has value 'default'

Good to know

Additionally, it is very important to know the difference between null coalesce ?? and shorthand ternary operator ?:. If we try to use shorthand ternary operator in a code block like this

$company_data = [];
$data = $company_data['xdoo'] ?: 'default';
// Notice: Undefined index: xdoo - PHP 7
// Warning: Undefined array key "xdoo" - PHP 8
// $data has value 'default'

it will still save value default to variable $data, but it will also render notice/warning.

In the case we have multiple fallbacks, we can use multiple ?? one after another. For example, let’s say we are expecting user input and sometimes we also have default override in the system. Code will look something like this

$user_input = [];
$deafult_data = [];
$data = $user_input['xdoo'] ?? $deafult_data['xdoo'] ?? 'default';
// $data has value 'default'

In case like this, we will first check if $user_input has data (isset and not null), if not set or null we will then check if we have system override in $default_data (isset and not null) and finally, if that is not set or is null we will set default value default.

If you want to level up your apps and your codebase, check out other posts on our blog xdoo.hr.

Do you need help with updating your old php codebase with our latest null coalescing operator optimizations?

For anything related to PHP or Laravel development send us your inquiry to [email protected]!


Leave a Reply

Your email address will not be published. Required fields are marked *