Skip to content

PHP Tutorial Operators

Nullsafe Operator

Lesson 56 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Reading a property of null is an error. When you have a chain like $order->customer->address->city, any link might be null, and the whole line can crash.

The nullsafe operator ?-> fixes that. If the value before it is null, PHP stops the chain and returns null.

index.php
PHP
<?php
class Address
{
    public function __construct(public string $city) {}
}

class User
{
    public function __construct(public ?Address $address = null) {}
}

$withAddress = new User(new Address("Lucknow"));
$withoutAddress = new User();

echo $withAddress->address?->city . "\n";
var_dump($withoutAddress->address?->city);
Output
OUTPUT
Lucknow
NULL

The second call would have caused an error with a plain ->. With ?->, it quietly returns null.

Without it

Before PHP 8.0 you had to write checks by hand.

index.php
PHP
<?php
class Address { public string $city = "Lucknow"; }
class User { public ?Address $address = null; }

$user = new User();

// Old style
$city = null;
if ($user !== null && $user->address !== null) {
    $city = $user->address->city;
}

// With the nullsafe operator
$city2 = $user?->address?->city;

var_dump($city, $city2);
Output
OUTPUT
NULL
NULL

Method calls too

It works with methods. The whole rest of the chain is skipped.

index.php
PHP
<?php
class Profile
{
    public function displayName(): string
    {
        return "Ada";
    }
}

$profile = null;

var_dump($profile?->displayName());
Output
OUTPUT
NULL

Combine it with ??

Use ?? to supply a default for the null result.

index.php
PHP
<?php
$profile = null;

echo $profile?->displayName() ?? "Anonymous";
Output
OUTPUT
Anonymous

What it does not do

  • It does not create missing array keys. For arrays, use ??.
  • It does not hide every error. If a value is null and you needed it, you now get a quiet null instead of a loud error. Use it where null is a normal, expected case.
info
Needs PHP 8.0
The nullsafe operator was added in PHP 8.0. A server running PHP 7 gives a parse error.