Last updated on November 23, 2022
The operator nullsafe
allows a developer to eliminate the null check for each property before going on to the next chain of the property or method. When the evaluation of one element in the chain fails, the execution of the entire chain aborts, and the entire chain evaluates to null.
It is very common 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 a null safe Operator you need to have an intermediate check as shown below.
if($user->address) {
echo $user->address->street;
}
Here is the example with the null safe operator:
echo $user?->address?->street;
You can avoid intermediate checks with this operator as shown above.