0
0
PHPprogramming~20 mins

Null coalescing operator in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Null Coalescing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of null coalescing with undefined variable
What is the output of this PHP code snippet?
PHP
<?php
$a = null;
echo $a ?? 'default';
?>
Adefault
Bnull
CNotice: Undefined variable
D0
Attempts:
2 left
💡 Hint
The null coalescing operator returns the right side if the left is null or undefined.
Predict Output
intermediate
2:00remaining
Null coalescing with multiple variables
What will this PHP code output?
PHP
<?php
$x = null;
$y = 0;
$z = 'hello';
echo $x ?? $y ?? $z;
?>
Ahello
B0
CNone
DNotice: Undefined variable
Attempts:
2 left
💡 Hint
The operator returns the first value that is not null.
Predict Output
advanced
2:00remaining
Null coalescing with array keys
What is the output of this PHP code?
PHP
<?php
$arr = ['a' => null, 'b' => 5];
echo $arr['a'] ?? $arr['b'] ?? 10;
?>
A10
Bnull
C5
DNotice: Undefined index
Attempts:
2 left
💡 Hint
Null coalescing operator checks if the key exists and is not null.
Predict Output
advanced
2:00remaining
Null coalescing with undefined array key
What will this PHP code output?
PHP
<?php
$arr = ['x' => 1];
echo $arr['y'] ?? 'missing';
?>
Amissing
BNotice: Undefined index
C1
Dnull
Attempts:
2 left
💡 Hint
Null coalescing operator avoids notice for undefined keys.
Predict Output
expert
2:00remaining
Null coalescing with function return values
What is the output of this PHP code?
PHP
<?php
function getValue() {
    return false;
}

$result = getValue() ?? 'default';
echo $result;
?>
ANotice: Undefined variable
Bdefault
C0
Dfalse
Attempts:
2 left
💡 Hint
Null coalescing checks for null, not false or zero.