The null safe operator helps you safely access properties or methods of an object that might be null, avoiding errors.
Null safe operator in PHP
$result = $object?->property; $result = $object?->method();
The ?-> operator checks if the object is not null before accessing.
If the object is null, the whole expression returns null instead of causing an error.
name from $user. Since $user is null, $name becomes null and no error happens.$user = null; $name = $user?->name; echo $name;
$user is an object with a name. The null safe operator returns 'Anna'.$user = new stdClass(); $user->name = 'Anna'; $name = $user?->name; echo $name;
$user = null; $length = $user?->getNameLength(); echo $length;
class User { public function getNameLength() { return strlen('Anna'); } } $user = new User(); $length = $user?->getNameLength(); echo $length;
This program shows how the null safe operator lets you access properties and methods safely when the object might be null.
<?php class User { public ?string $name; public function getNameLength(): ?int { return $this->name ? strlen($this->name) : null; } } $user1 = new User(); $user1->name = 'John'; $user2 = null; // Using null safe operator echo $user1?->name . "\n"; // Prints 'John' echo $user2?->name . "\n"; // Prints nothing (null) echo $user1?->getNameLength() . "\n"; // Prints 4 echo $user2?->getNameLength() . "\n"; // Prints nothing (null)
The null safe operator was introduced in PHP 8.0.
It helps avoid many if checks for null before accessing objects.
If the object is null, the whole expression returns null, so be careful when printing or using the result.
The null safe operator ?-> lets you access properties or methods safely on objects that might be null.
It returns null instead of causing an error if the object is null.
This makes your code shorter and easier to read when dealing with optional objects.
Practice
?-> do in PHP?Solution
Step 1: Understand the null safe operator purpose
The null safe operator?->is used to safely access properties or methods of an object that might be null.Step 2: Behavior when object is null
If the object is null, instead of throwing an error, it returns null immediately without calling the property or method.Final Answer:
It allows accessing a property or method only if the object is not null, otherwise returns null. -> Option AQuick Check:
Null safe operator = safe access or null return [OK]
- Assuming it throws an error on null
- Confusing it with the regular object operator ->
- Thinking it converts null to empty string
Solution
Step 1: Recall the correct null safe operator syntax
The null safe operator is written as?->immediately after the variable name.Step 2: Check each option
$user?->getName(); uses?->correctly. Options B, C, and D have incorrect placements or missing characters.Final Answer:
$user?->getName(); -> Option CQuick Check:
Correct syntax = ?-> [OK]
- Placing ? after -> or in wrong position
- Using only ? without ->
- Confusing with ternary operator syntax
$user = null; echo $user?->getName() ?? 'Guest';
Solution
Step 1: Evaluate the null safe operator usage
$user is null, so$user?->getName()returns null safely without error.Step 2: Apply null coalescing operator
The expression uses?? 'Guest', so since left side is null, it returns 'Guest'.Final Answer:
Guest -> Option DQuick Check:
Null safe + null coalescing = fallback value [OK]
- Expecting an error when calling method on null
- Confusing null safe with regular -> operator
- Ignoring the null coalescing fallback
$order = null; echo $order?->getItem()->getName();
Solution
Step 1: Analyze the null safe operator chaining
The first null safe operator?->safely callsgetItem()on $order which is null, so it returns null.Step 2: Check the next method call
ThengetName()is called on the result ofgetItem()without null safe operator, causing an error because it's called on null.Final Answer:
Error becausegetItem()is called on null without null safe operator. -> Option BQuick Check:
Chained calls need ?-> on each nullable step [OK]
- Assuming one ?-> covers all chained calls
- Ignoring that getName() is called on null
- Thinking null safe operator fixes all errors automatically
class Profile {
public ?string $nickname = null;
}
class User {
public ?Profile $profile = null;
}
$user = new User();
$user->profile = new Profile();
$user->profile->nickname = null;
echo $user?->profile?->nickname ?? 'No nickname';Solution
Step 1: Trace object properties and null safe operator
$user is an object, $user->profile is an object, but $user->profile->nickname is null.Step 2: Evaluate null safe operator and null coalescing
The expression$user?->profile?->nicknamesafely returns null because nickname is null. Then?? 'No nickname'returns 'No nickname'.Final Answer:
No nickname -> Option AQuick Check:
Null safe returns null, ?? fallback used [OK]
- Expecting null output instead of fallback string
- Thinking null safe operator throws error on null property
- Confusing null and empty string values
Ready to prove it?
Pass the test and this pattern moves unknown
