Challenge - 5 Problems
Null Coalescing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of null coalescing with undefined variable
What is the output of this PHP code snippet?
PHP
<?php $a = null; echo $a ?? 'default'; ?>
Attempts:
2 left
💡 Hint
The null coalescing operator returns the right side if the left is null or undefined.
✗ Incorrect
Since $a is explicitly set to null, the null coalescing operator returns the right side 'default'.
❓ Predict Output
intermediate2:00remaining
Null coalescing with multiple variables
What will this PHP code output?
PHP
<?php $x = null; $y = 0; $z = 'hello'; echo $x ?? $y ?? $z; ?>
Attempts:
2 left
💡 Hint
The operator returns the first value that is not null.
✗ Incorrect
Here, $x is null, so it checks $y which is 0 (not null), so it outputs 0.
❓ Predict Output
advanced2: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; ?>
Attempts:
2 left
💡 Hint
Null coalescing operator checks if the key exists and is not null.
✗ Incorrect
The key 'a' exists but is null, so it moves to 'b' which is 5 and outputs 5.
❓ Predict Output
advanced2:00remaining
Null coalescing with undefined array key
What will this PHP code output?
PHP
<?php $arr = ['x' => 1]; echo $arr['y'] ?? 'missing'; ?>
Attempts:
2 left
💡 Hint
Null coalescing operator avoids notice for undefined keys.
✗ Incorrect
Since 'y' key does not exist, it returns the right side 'missing' without error.
❓ Predict Output
expert2: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; ?>
Attempts:
2 left
💡 Hint
Null coalescing checks for null, not false or zero.
✗ Incorrect
The function returns false which is not null, so the operator returns false and echoes it as empty string.