0
0
PHPprogramming~20 mins

Isset, empty, and is_null behavior in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Variable State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of isset() with different variable states
What is the output of the following PHP code?
PHP
<?php
$a = null;
$b = 0;
$c = '';
$d = false;

var_dump(isset($a));
var_dump(isset($b));
var_dump(isset($c));
var_dump(isset($d));
?>
A
bool(false)
bool(true)
bool(true)
bool(true)
B
bool(false)
bool(false)
bool(false)
bool(false)
C
bool(true)
bool(true)
bool(true)
bool(true)
D
bool(false)
bool(true)
bool(false)
bool(true)
Attempts:
2 left
💡 Hint
Remember that isset() returns false only if the variable is null or not set.
Predict Output
intermediate
2:00remaining
Behavior of empty() with various values
What will be the output of this PHP code snippet?
PHP
<?php
$x = 0;
$y = '0';
$z = null;
$w = [];

var_dump(empty($x));
var_dump(empty($y));
var_dump(empty($z));
var_dump(empty($w));
?>
A
bool(true)
bool(false)
bool(true)
bool(false)
B
bool(false)
bool(false)
bool(false)
bool(false)
C
bool(true)
bool(true)
bool(true)
bool(true)
D
bool(false)
bool(true)
bool(false)
bool(true)
Attempts:
2 left
💡 Hint
empty() returns true for values considered 'empty' like 0, '0', null, and empty arrays.
Predict Output
advanced
2:00remaining
Difference between is_null() and isset()
What is the output of this PHP code?
PHP
<?php
$var = null;

var_dump(is_null($var));
var_dump(isset($var));

unset($var);

var_dump(is_null($var));
var_dump(isset($var));
?>
A
bool(true)
bool(false)
bool(true)
bool(false)
B
bool(true)
bool(true)
bool(false)
bool(false)
C
bool(true)
bool(false)
Notice: Undefined variable
bool(false)
D
bool(false)
bool(false)
bool(true)
bool(true)
Attempts:
2 left
💡 Hint
is_null() on an unset variable triggers a notice but returns true; isset() returns false.
Predict Output
advanced
2:00remaining
Output of empty() on undefined variable
What will this PHP code output?
PHP
<?php
var_dump(empty($undefinedVar));
var_dump(isset($undefinedVar));
?>
A
bool(true)
bool(true)
B
Notice: Undefined variable
bool(false)
C
bool(false)
bool(false)
D
bool(true)
bool(false)
Attempts:
2 left
💡 Hint
empty() does not raise a notice for undefined variables, but isset() returns false.
🧠 Conceptual
expert
3:00remaining
Understanding combined behavior of isset(), empty(), and is_null()
Given the following PHP code, what is the output?
PHP
<?php
$var1 = 0;
$var2 = null;
$var3 = '';

if (isset($var1) && !empty($var1)) {
    echo 'A';
}
if (is_null($var2) || empty($var2)) {
    echo 'B';
}
if (!isset($var3) || empty($var3)) {
    echo 'C';
}
?>
AABC
BBC
CAC
DAB
Attempts:
2 left
💡 Hint
Check each condition carefully considering how isset(), empty(), and is_null() behave with 0, null, and empty string.