Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Nullsafe operator in PHP 8

$country =  null;

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

  if ($user !== null) {
    $address = $user->getAddress();
 
    if ($address !== null) {
      $country = $address->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.  */
// Example
$country = $session?->user?->getAddress()?->country;
Source by www.php.net #
 
PREVIOUS NEXT
Tagged: #Nullsafe #operator #PHP
ADD COMMENT
Topic
Name
3+6 =