Posted by Arjun on Monday 3rd August 2020

nullsafe operator - PHP 8


The nullsafe Operator allows a developer to eliminate the null check for each property before going onto the next chain of the property or method.

It is very common to only want to call a method or fetch a property on the result of an expression if it is not null.

For example, to access user address street details, without null safe Operator you need to have an intermediate check as shown below.

   if($user->address) {
    echo $user->address->street;

Here the example with nulsafe operator:

    echo $user?->address?->street;

see you can avoid intermediate checks with this operator.