Challenge - 5 Problems
PHP Variable State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)); ?>
Attempts:
2 left
💡 Hint
Remember that isset() returns false only if the variable is null or not set.
✗ Incorrect
The isset() function returns false if the variable is null or not set. For other values like 0, empty string, or false, it returns true.
❓ Predict Output
intermediate2: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)); ?>
Attempts:
2 left
💡 Hint
empty() returns true for values considered 'empty' like 0, '0', null, and empty arrays.
✗ Incorrect
The empty() function returns true for values that PHP considers empty: 0, '0', null, empty array, false, and empty string.
❓ Predict Output
advanced2: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)); ?>
Attempts:
2 left
💡 Hint
is_null() on an unset variable triggers a notice but returns true; isset() returns false.
✗ Incorrect
is_null() returns true if the variable is null or unset (triggers notice when unset). isset() returns false if null or unset. Option A shows the var_dump outputs; notice is additional error output.
❓ Predict Output
advanced2:00remaining
Output of empty() on undefined variable
What will this PHP code output?
PHP
<?php var_dump(empty($undefinedVar)); var_dump(isset($undefinedVar)); ?>
Attempts:
2 left
💡 Hint
empty() does not raise a notice for undefined variables, but isset() returns false.
✗ Incorrect
empty() returns true for undefined variables without raising a notice. isset() returns false for undefined variables.
🧠 Conceptual
expert3: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'; } ?>
Attempts:
2 left
💡 Hint
Check each condition carefully considering how isset(), empty(), and is_null() behave with 0, null, and empty string.
✗ Incorrect
First condition: isset(0) true, but empty(0) true so !empty(0) false → no 'A'.
Second: is_null(null) true → 'B'.
Third: !isset('') false, but empty('') true → 'C'.
Output: BC