1

Is there a way to use Nullsafe operator conditions in php?

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144

2 Answers2

5

PHP 7

$country =  null;

if ($session !== null) {
  $user = $session->user;

  if ($user !== null) {
    $address = $user->getAddress();
 
    if ($address !== null) {
      $country = $address->country;
    }
  }
}

PHP 8

$country = $session?->user?->getAddress()?->country;

Instead of null check conditions, you can now use a chain of calls with the new nullsafe operator. When the evaluation of one element in the chain fails, the execution of the entire chain aborts and the entire chain evaluates to null.

source: https://www.php.net/releases/8.0/en.php

Jsowa
  • 9,104
  • 5
  • 56
  • 60
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
  • Please always look for duplicate pages before answering. – mickmackusa Nov 27 '20 at 22:20
  • If you feel this answer would add unique value to the original, please transfer to the older page and delete this one. Ah, I now realize that you self-answered -- you really should have searched more before going to this effort. – mickmackusa Nov 27 '20 at 22:45
  • note that the `?->` version will fail if the field is not defined. For example $session?->user will throw a warning that property user is undefined if it is not present on the $session variable. In those cases you would still need to use ?? operator – Holonaut Apr 11 '22 at 09:57
0

Nullsafe operator allows you to chain the calls avoiding checking whether every part of chain is not null (methods or properties of null variables).

PHP 8.0

$city = $user?->getAddress()?->city

Before PHP 8.0

$city = null;
if($user !== null) {
    $address = $user->getAddress();
    if($address !== null) {
        $city = $address->city;
    }
}

With null coalescing operator (it doesn't work with methods):

$city = null;
if($user !== null) {
    $city = $user->getAddress()->city ?? null;
}

Nullsafe operator suppresses errors:

Warning: Attempt to read property "city" on null in Fatal error:

Uncaught Error: Call to a member function getAddress() on null

However it doesn't work with array keys:

$user['admin']?->getAddress()?->city //Warning: Trying to access array offset on value of type null

$user = [];
$user['admin']?->getAddress()?->city //Warning: Undefined array key "admin"
Jsowa
  • 9,104
  • 5
  • 56
  • 60
  • I was amazed to see that this feature was proposed in the question waaaaay back in 2012 (I don't know if it was proposed earlier elsewhere) and it only managed to become a feature in 2020! – mickmackusa Nov 27 '20 at 22:44
  • 1
    If you feel this answer would add unique value to the original, please transfer to the older page and delete this one. – mickmackusa Nov 27 '20 at 22:45
  • I added but maybe better description of duplicate would be this link https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Jsowa Nov 27 '20 at 23:13
  • I personally super-hate ridiculously long canonicals. Horrible UX for researchers. – mickmackusa Nov 27 '20 at 23:14