Nullsafe Operator

// As of PHP 8.0.0, this line:
$result = $repo?->getUser(5)?->name;
// Equivalent to the following code:
if (is\_null($repo)) {
    $result = null;
} else {
    $user = $repository->getUser(5);
    if (is\_null($user)) {
        $result = null;
    } else {
        $result = $user->name;
    }
}

See also: Nullsafe Operator

Comments