Nullsafe

A nullsafe operator is able to carry a function or fail graciously to null. In particular, it won’t stop the execution with a fatal error.

There are two PHP operators that are nullsafe:

  • nullsafe object operator ?->

  • nullsafe-coalesce ??.

<?php

$a = foo();

$b = $a?->method();

$b = $a?->chain1?->chain2?->method();

// foo may return null
function foo() : ?A { }

?>

See also Mastering Null Safety in PHP 8: A Comprehensive Guide to Using the Null Safe Operator and PHP 8.0: Null-safe operator.

Related : Object Operator ->, Coalesce Operator, Streamlining

Added in PHP 8.0