Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php 8 major changes - Nullsafe operator

BEFORE
$country =  null;

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

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

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;
Comment

PREVIOUS NEXT
Code Example
Php :: valid number in excel php 
Php :: laravel create table with model command line 
Php :: how to increase request timeout in laravel 
Php :: get today date in php 
Php :: verificare esistenza file in php 
Php :: php redirect after specific seconds 
Php :: laravel get class name 
Php :: Turning a StdClass object into an array 
Php :: centos search directory php.exe 
Php :: DB::rollback() 
Php :: join 2 tables laravel 
Php :: check is array laravel 
Php :: install ext-ldap php 7.2 
Php :: laravel on delete set null 
Php :: php serve 
Php :: code php ajout heure 
Php :: smarty if 
Php :: Laravel Unable to migrate or Make Seeds 
Php :: current loggedin user laravel 
Php :: Problem 1 - phpspec/prophecy is locked to version 1.13.0 and an update of this package was not requested. - phpspec/prophecy 1.13.0 requires php ^7.2 || ~8.0, <8.1 - your php version (8.1.2) does not satisfy that requirement. 
Php :: minuscule chaine php 
Php :: migrate to an existing table in laravel commad 
Php :: php subtract seconds from datetime 
Php :: get only date in laravel 
Php :: htmlspecialchars() expects parameter 1 to be string 
Php :: laravel blade time difference 
Php :: laravel validation allow empty array 
Php :: php check if function exists 
Php :: php usort keep keys 
Php :: ext-curl install php 7.2 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =