Backend Development

PHP The Null Coalescing Operator and The Null Safe Operator

PHP The Null Coalescing Operator and The Null Safe Operator

Two important operators in PHP 7 and PHP 8 when working with null values is the null coalescing and null safe operators.

 

 

Dealing with null values is a common aspect in any programming language, and in PHP also there are many cases where we have to check for null values.

The most common tasks when manipulating nulls is to check for variable existence, in PHP typically we tend to use something like PHP isset() function in a ternary expression:

$canLogin = isset($check) ? "yes" : "no";

This is a common expression in PHP, and for this the null coalescing operator we can shorten this expression as follows:

$canLogin = $check ?? 'no';   // no

As you see we omit the first side from the ternary expression and replaced the isset() call with the ?? operator. If the condition is correct that it will return the value of the condition otherwise it will return the else part which in the example is ‘no’. If we assign a value to variable $check it will return the value of $check:

$check = 'yes';

$canLogin = $check ?? 'no';     // yes

 

Null Coalescing With Arrays

The null coalescing operator is very beneficial when checking items existence in arrays, consider this example:

$user = [
    'name' => 'John Doe',
    'location' => [
        'country' => 'UK'
    ]
];

Without using null coalescing operator we can do something like this:

$username = isset($user['name']) ? $user['name'] : 'Annonymous';   

But with the null coalescing we can rewrite it like so:

$username = $user['name'] ?? 'Annonymous';     // John Doe

In case the array key doesn’t exist it will simply return the right-hand side:

$age = $user['age'] ?? 'unidentified';   // unidentified

Also we can use it for nested array items in multi-dimensional arrays:

$country = $user['location']['country'] ?? 'No country';    // UK
$city = $user['location']['city'] ?? 'No city';    // No city

If we cast this array to an object, the same chaining technique can be used:

$userObj = (object) $user;

$city = $userObj->location->city ?? 'No city';

 

Chaining Null Coalescing

The null coalescing operator can be chained to form complex expressions:

$userId = $_REQUEST['comments'] ?? $_REQUEST['comments']['user_id'] ?? 0;

Which is the same as:

$userId = isset($_REQUEST['comments']) ? (isset($_REQUEST['comments']['user_id']) ? $_REQUEST['comments']['user_id'] : 0) : 0;

 

What about chaining method calls with null coalescing operator? The answer is no, for this the null safe operator can be used.

 

Null Safe Operator

The null safe operator is another operator released in PHP 8, that is some kind similar to coalescing operator, but solves another problem.

Consider this example:

$foo?->bar?->baz
$user?->getUserDetails()?->getSchools()[0]->name

The ?-> symbol refer to the null safe operator. You might encountered the null safe operator in other programming languages. The null safe operator works by ensuring that the left-hand result is not null before accessing specific property or method.

In this code if the $user is not null then it will continue to invoke getUserDetails(), in turn if the result is not null it will continue getSchools and so on.

If the $user is null or any forwarding call is null then the result is null, this is why the term “null safe” which i didn’t throw any errors when attempt to call a property on a nullable.

Example:

class User
{
    public function __construct(public UserDetails $userDetails) 
    {}
    
    
    public function getUserDetails() : ?UserDetails
    {
    	return null;
    }
}

class UserDetails
{
    public function __construct(public int $userId)
    {
    	
    }
    
    public function getSchools() : ?array
    {
        return null;
    }
}

class UserSchool
{
    public function __construct(public string $name)
    {
    }
}

In this code imagine we want to retrieve the user schools:

$user = new User(new UserDetails(2));

$schools = $user->getUserDetails()->getSchools(); 

When running this code it will throw an error because the getUserDetails() return null

PHP Fatal error:  Uncaught Error: Call to a member function getSchools() on null in ....

Now let’s use the null safe operator:

$schools = $user?->getUserDetails()?->getSchools(); 

The code will continue running normally without any errors.

If you are working with Laravel or symfony frameworks you already familiar with model relationships. The most common use for null safe operator when trying to access related models.

 

The question is can we use either of the null coalescing operator or the null safe? the answer is both operators handle different use cases and solve different problems.

For example in the above code if we replaced the null safe by the null coalescing:

$schools = $user->getUserDetails()->getSchools() ?? null; 

This code will break and throw the same error as above as the null coalescing doesn’t guarantee that any of the method calls is not null but the null safe do.

So the null safe operator mostly used with method calls that expected to return null. The null coalescing operator mostly used with undefined variables and arrays.

 

0 0 votes
Article Rating

What's your reaction?

Excited
1
Happy
0
Not Sure
1
Confused
0

You may also like

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments