php 8 features code example

Example 1: php 8

latest version of php.
see new features at https://php.watch/versions/8.0

Example 2: php8

// php 8 Union type
class Number {
  public function __construct(
    private int|float $number
  ) {}
}

new Number('NaN'); // TypeError

Example 3: php 8 " php by Upset Unicorn on Jan 13 2021 Donate Comment

<?= is a shorthand for <?php echo $a; ?>

Example 4: php8

// php8 is null or not
$country = $session?->user?->getAddress()?->country;

//-----------------------------
// php7 below.
$country =  null;

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

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

Tags:

Php Example