0
0
PHPprogramming~20 mins

Null type and its meaning in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Null Mastery in PHP
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 with null?

Look at this PHP code. What will it print?

PHP
<?php
$value = null;
if (is_null($value)) {
    echo 'Yes';
} else {
    echo 'No';
}
?>
ANo
BYes
Cnull
DError
Attempts:
2 left
💡 Hint

Check what is_null() does when the variable is null.

Predict Output
intermediate
2:00remaining
What is the output when comparing null with empty string?

What will this PHP code print?

PHP
<?php
var_dump(null == '');
var_dump(null === '');
?>
Abool(false) bool(false)
Bbool(true) bool(true)
Cbool(true) bool(false)
Dbool(false) bool(true)
Attempts:
2 left
💡 Hint

Remember that == checks value equality with type juggling, === checks type and value.

🔧 Debug
advanced
2:00remaining
Why does this code cause a warning with null?

Find the reason for the warning in this PHP code:

PHP
<?php
$value = null;
echo strlen($value);
?>
AWarning because strlen() expects a string, null is not a string
BWarning because null cannot be echoed directly
CNo warning, it prints 0
DFatal error because null is undefined
Attempts:
2 left
💡 Hint

Check what type strlen() expects as input.

🧠 Conceptual
advanced
2:00remaining
What does the null coalescing operator do in PHP?

Choose the best description of the null coalescing operator ?? in PHP.

AAssigns null to the left variable if the right is null
BReturns true if both values are null
CReturns the left value if it is not null, otherwise returns the right value
DChecks if both values are equal and not null
Attempts:
2 left
💡 Hint

Think about how to provide a default value when a variable might be null.

Predict Output
expert
2:00remaining
What is the output of this PHP code mixing null and arrays?

What will this PHP code print?

PHP
<?php
$array = ['a' => null, 'b' => 0, 'c' => false];
$count = 0;
foreach ($array as $key => $value) {
    if ($value == null) {
        $count++;
    }
}
echo $count;
?>
A1
B0
C3
D2
Attempts:
2 left
💡 Hint

Remember that == null is true for null and false, but not for 0 in PHP.