📖 Scenario: Imagine you have a list of people, and each person may or may not have a car. You want to check the color of each person's car safely without causing errors if the car is missing.
🎯 Goal: Build a PHP script that uses the null safe operator to safely access the car color of each person.
📋 What You'll Learn
Create a class Car with a public property color.
Create a class Person with a public property car that can be null or a Car object.
Create an array people with some Person objects, some with cars and some without.
Use the null safe operator ?-> to get the car color safely for each person.
Print the car color or "No car" if the person has no car.
💡 Why This Matters
🌍 Real World
In real applications, objects may or may not have related objects. The null safe operator helps avoid errors when accessing properties of objects that might be missing.
💼 Career
Understanding the null safe operator is important for writing clean and error-free PHP code, especially when working with APIs, databases, or complex data structures.
Progress0 / 4 steps
1
Create classes and people array
Create a class called Car with a public property color. Create a class called Person with a public property car. Then create an array called people with three Person objects: the first has a Car with color "red", the second has no car (null), and the third has a Car with color "blue".
PHP
Hint
Define the classes with public properties and create the array with the exact objects and values.
2
Add a variable for the people array
Create a variable called people that holds the array of Person objects you created in Step 1.
PHP
Hint
The $people variable should already be set from Step 1.
3
Use null safe operator to get car colors
Use a foreach loop with variable $person to iterate over $people. Inside the loop, create a variable $color that uses the null safe operator ?-> to get the color property of the car property of $person. If the car is missing, $color should be null.
PHP
Hint
Use ?-> to safely access color from car.
4
Print car colors or 'No car'
Inside the foreach loop, print the value of $color if it is not null. Otherwise, print "No car". Use echo to display the output followed by a newline.
PHP
Hint
Use the null coalescing operator ?? to print $color or "No car".
Practice
(1/5)
1. What does the null safe operator ?-> do in PHP?
easy
A. It allows accessing a property or method only if the object is not null, otherwise returns null.
B. It throws an error if the object is null when accessing a property or method.
C. It converts null values to empty strings automatically.
D. It forces the object to be non-null before accessing properties or methods.
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 A
Quick Check:
Null safe operator = safe access or null return [OK]
Hint: Think: safe access without errors returns null if null [OK]
Common Mistakes:
Assuming it throws an error on null
Confusing it with the regular object operator ->
Thinking it converts null to empty string
2. Which of the following is the correct syntax to use the null safe operator in PHP?
easy
A. $user.?getName();
B. $user->?getName();
C. $user?->getName();
D. $user?getName();
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 C
Quick Check:
Correct syntax = ?-> [OK]
Hint: Remember: question mark before arrow ?-> is correct [OK]
Common Mistakes:
Placing ? after -> or in wrong position
Using only ? without ->
Confusing with ternary operator syntax
3. What will be the output of the following PHP code?
$user = null;
echo $user?->getName() ?? 'Guest';
medium
A. Empty string
B. Error: Call to a member function getName() on null
C. null
D. 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'.
B. Error because getItem() is called on null without null safe operator.
C. Syntax error due to missing semicolon.
D. Error because null safe operator cannot be chained.
Solution
Step 1: Analyze the null safe operator chaining
The first null safe operator ?-> safely calls getItem() on $order which is null, so it returns null.
Step 2: Check the next method call
Then getName() is called on the result of getItem() without null safe operator, causing an error because it's called on null.
Final Answer:
Error because getItem() is called on null without null safe operator. -> Option B
Quick Check:
Chained calls need ?-> on each nullable step [OK]
Hint: Use ?-> before every nullable method call in chain [OK]
Common Mistakes:
Assuming one ?-> covers all chained calls
Ignoring that getName() is called on null
Thinking null safe operator fixes all errors automatically
5. Given the following PHP code, what will be the output?
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';
hard
A. No nickname
B. null
C. Error: Cannot access property on null
D. Empty string
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?->nickname safely returns null because nickname is null. Then ?? 'No nickname' returns 'No nickname'.