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
- Safe Navigation: Nullsafe operators prevent errors when accessing properties or methods on null objects.
- Concise Syntax: They reduce the need for verbose null checks.
- Improved Readability: Code becomes cleaner and easier to understand.
- Chaining Support: Nullsafe operators can be used in chained method or property calls.
- Error Prevention: They help avoid runtime errors caused by null references.
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.
<?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.
<?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.
<?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.
<?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.
<?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.
<?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.
<?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.
<?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.
<?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.
<?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.
<?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
List all PHP tutorials.