ZetCode

PHP Nullsafe Operators

last modified October 22, 2026

In this article, we show how to use nullsafe operators in PHP. Nullsafe operators, introduced in PHP 8.0, provide a concise way to handle null values in object chains. They allow you to safely navigate through potentially null objects without causing errors.

Main Features of PHP Nullsafe Operators

Nullsafe operators are represented by the ?-> syntax. They return null if the object before the operator is null, instead of throwing an error.

Basic Usage of Nullsafe Operators

The following example demonstrates how to use nullsafe operators in PHP.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Address $address = null;
}

class Address {
    public string $city;
}

$user = new User();
echo $user->address?->city ?? 'Unknown';

In this program, the User class has a nullable Address property. The nullsafe operator ?-> is used to safely access the city property. If address is null, null is returned, and the ?? operator provides a default value.

$ php main.php
Unknown

Nullsafe Operator with Method Calls

The following example demonstrates how to use nullsafe operators with methods.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public function getBio(): string {
        return "PHP Developer";
    }
}

$user = new User();
echo $user->profile?->getBio() ?? 'No bio available';

In this program, the Profile class has a getBio method. The nullsafe operator is used to call this method safely. If profile is null, the default value is returned.

$ php main.php
No bio available

Chaining Nullsafe Operators

The following example demonstrates how to chain nullsafe operators.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public ?Address $address = null;
}

class Address {
    public string $city = "New York";
}

$user = new User();
echo $user->profile?->address?->city ?? 'Unknown';

In this program, the nullsafe operator is used to safely navigate through multiple levels of potentially null objects. If any object in the chain is null, the result is null.

$ php main.php
Unknown

Nullsafe Operator with Arrays

The following example demonstrates how to use nullsafe operators with arrays.

main.php
<?php

declare(strict_types=1);

class User {
    private ?array $preferences = null;

    public function getPreferences(): ?array
    {
        return $this->preferences;
    }
}

$user = new User();
echo $user->getPreferences()['theme'] ?? 'Default theme';

There is no nullsafe syntax for array access; ?[ is a parse error. Reading an element of a null value yields null without an error, so the null coalescing operator is enough to supply the default value when getPreferences returns null. When the array comes from a possibly null object, combine both: $user?->getPreferences()['theme'] ?? 'Default theme'.

$ php main.php
Default theme

Nullsafe Operator with Static Methods

The following example demonstrates how to use nullsafe operators with static methods.

main.php
<?php

declare(strict_types=1);

class User {
    public function __construct(public string $name) {}

    public static function find(int $id): ?User
    {
        return $id === 1 ? new User("John Doe") : null;
    }
}

echo User::find(2)?->name ?? 'Anonymous', "\n";
echo User::find(1)?->name ?? 'Anonymous', "\n";

In this program, the static method find returns a User or null. The nullsafe operator is applied to the value returned by the static call. For find(2) the result is null, so the property access is skipped and the default value is used. The operator cannot be applied to the class name itself; User?::find() is not valid.

$ php main.php
Anonymous
John Doe

Nullsafe Operator with Null Coalescing

The following example demonstrates how to combine nullsafe operators with the null coalescing operator.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public ?string $bio = null;
}

$user = new User();
echo $user->profile?->bio ?? 'No bio available';

In this program, the nullsafe operator is used to safely access the bio property. If profile or bio is null, the default value is returned.

$ php main.php
No bio available

Nullsafe Operator with Function Calls

The following example demonstrates how to use nullsafe operators with function calls.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public function getBio(): string {
        return "PHP Developer";
    }
}

function displayBio(?User $user): void {
    echo $user?->profile?->getBio() ?? 'No bio available';
}

$user = new User();
displayBio($user);

In this program, the nullsafe operator is used within a function to safely access the getBio method. If user or profile is null, the default value is returned.

$ php main.php
No bio available

Nullsafe Operator with Nested Objects

The following example demonstrates how to use nullsafe operators with deeply nested objects.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public ?Address $address = null;
}

class Address {
    public ?string $city = "New York";
}

$user = new User();
echo $user->profile?->address?->city ?? 'Unknown';

In this program, the nullsafe operator is used to safely navigate through multiple levels of potentially null objects. If any object in the chain is null, the result is null.

$ php main.php
Unknown

Nullsafe Operator with Optional Parameters

The following example demonstrates how to use nullsafe operators with optional parameters.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public function getBio(?string $default = null): string {
        return $default ?? "PHP Developer";
    }
}

$user = new User();
echo $user->profile?->getBio('No bio available') ?? 'Anonymous';

In this program, the nullsafe operator is used to safely call a method with optional parameters. If profile is null, the default value is returned.

$ php main.php
Anonymous

Nullsafe Operator with Object Initialization

The following example shows nullsafe operators with an initialized object.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public string $name = "John Doe";
}

$user = new User();
$user->profile = new Profile();
echo $user->profile?->name ?? 'Unknown';

In this program, the nullsafe operator accesses a property after initializing the object. If profile were null, the default applies.

$ php main.php
John Doe

Nullsafe Operator with Multiple Chains

The following example uses multiple nullsafe chains in one expression.

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
    public ?Settings $settings = null;
}

class Profile {
    public string $role = "Developer";
}

class Settings {
    public string $mode = "Dark";
}

$user = new User();
$user->profile = new Profile();

echo ($user->profile?->role ?? 'Unknown') . " " .
    ($user->settings?->mode ?? 'Default');

In this program, two nullsafe chains fetch properties. Each chain has its own default value in parentheses; without them, the concatenation operator would bind tighter than ?? and the defaults would apply to the whole string. The profile is set, but settings is null.

$ php main.php
Developer Default

Source

PHP Nullsafe Operators - Documentation

In this article, we have shown how to use nullsafe operators in PHP for safe navigation through potentially null objects. Nullsafe operators are a powerful tool for writing cleaner and more robust code.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP tutorials.