0
0
PHPprogramming~20 mins

Null safe operator in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Null Safe Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code using the null safe operator?

Consider the following PHP code snippet. What will it output?

PHP
<?php
class Profile {
    public ?string $email = null;
}
class User {
    public ?Profile $profile = null;
}

$user = new User();
echo $user->profile?->email ?? 'No email';
?>
ANo email
BError: Trying to access property on null
Cnull
DEmpty string
Attempts:
2 left
💡 Hint

The null safe operator returns null if the left side is null, so the null coalescing operator will provide the fallback.

Predict Output
intermediate
2:00remaining
What does this PHP code print when using the null safe operator with nested objects?

Given the code below, what will be printed?

PHP
<?php
class Engine {
    public ?string $type = 'V8';
}
class Car {
    public ?Engine $engine = null;
}

$car = new Car();
echo $car->engine?->type ?? 'No engine';
?>
ANo engine
BV8
Cnull
DError: Undefined property
Attempts:
2 left
💡 Hint

Check if engine is set before accessing type.

Predict Output
advanced
2:00remaining
What is the output of this PHP code using null safe operator with method chaining?

Analyze the code below and select the correct output.

PHP
<?php
class Address {
    public ?string $city = 'Paris';
}
class User {
    public ?Address $address = null;
    public function getAddress(): ?Address {
        return $this->address;
    }
}

$user = new User();
echo $user->getAddress()?->city ?? 'Unknown city';
?>
Anull
BParis
CUnknown city
DFatal error: Call to a member function on null
Attempts:
2 left
💡 Hint

Consider what getAddress() returns and how the null safe operator works with method calls.

Predict Output
advanced
2:00remaining
What is the output of this PHP code using null safe operator on null?

What will this code output?

PHP
<?php
class Book {
    public ?string $title = 'PHP Guide';
}

$book = null;
echo $book?->title;
?>
AFatal error: Call to undefined method string::title()
BNo output, prints nothing
CParse error: syntax error, unexpected '?->'
DFatal error: Call to a member function title() on null
Attempts:
2 left
💡 Hint

The null safe operator short-circuits when the left side is null and does not evaluate the right side.

🧠 Conceptual
expert
3:00remaining
How many items are in the resulting array after this PHP code runs?

Consider this PHP code using null safe operator inside a loop. How many items will the $results array contain?

PHP
<?php
class Item {
    public ?string $name;
    public function __construct(?string $name) {
        $this->name = $name;
    }
}

$items = [new Item('A'), null, new Item(null), new Item('D')];
$results = [];
foreach ($items as $item) {
    $results[] = $item?->name ?? 'Unknown';
}
print_r($results);
?>
A1
B3
C2
D4
Attempts:
2 left
💡 Hint

Count how many times the loop runs and what values are added to $results.