Bird
Raised Fist0
PHPprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following statements about isset in PHP is true?
easy
A. isset returns true even if the variable is null.
B. isset returns false if the variable is not set or is null.
C. isset checks if a variable is empty or zero.
D. isset returns true only if the variable is a string.

Solution

  1. Step 1: Understand isset behavior

    isset returns false if the variable does not exist or is set to null.
  2. Step 2: Analyze options

    isset returns false if the variable is not set or is null. correctly states this behavior: isset returns false if the variable is not set or is null. Options A, C, and D are incorrect because isset returns false for null, does not check emptiness or zero, and is not limited to strings.
  3. Final Answer:

    isset returns false if the variable is not set or is null. -> Option B
  4. Quick Check:

    isset false if null or unset [OK]
Hint: Remember: isset false if variable missing or null [OK]
Common Mistakes:
  • Thinking isset returns true for null variables
  • Confusing isset with empty
  • Assuming isset checks variable type
2. Which of the following is the correct syntax to check if a variable $var is empty in PHP?
easy
A. if (empty($var)) { ... }
B. if (empty->$var) { ... }
C. if (is_empty($var)) { ... }
D. if (empty $var) { ... }

Solution

  1. Step 1: Recall empty syntax

    The empty function requires parentheses around the variable: empty($var).
  2. Step 2: Check each option

    if (empty($var)) { ... } uses correct syntax. if (empty $var) { ... } misses parentheses, C uses a non-existent function, and B uses invalid syntax.
  3. Final Answer:

    if (empty($var)) { ... } -> Option A
  4. Quick Check:

    empty requires parentheses [OK]
Hint: Use parentheses with empty: empty($var) [OK]
Common Mistakes:
  • Omitting parentheses with empty
  • Using is_empty instead of empty
  • Using arrow operator incorrectly
3. What will be the output of the following PHP code?
$a = 0;
var_dump(isset($a));
var_dump(empty($a));
var_dump(is_null($a));
medium
A. bool(true)
bool(false)
bool(false)
B. bool(false)
bool(true)
bool(true)
C. bool(false)
bool(false)
bool(true)
D. bool(true)
bool(true)
bool(false)

Solution

  1. Step 1: Evaluate isset($a) when $a = 0

    $a is set and not null, so isset($a) returns true.
  2. Step 2: Evaluate empty($a) when $a = 0

    Zero is considered empty in PHP, so empty($a) returns true.
  3. Step 3: Evaluate is_null($a) when $a = 0

    $a is integer zero, not null, so is_null($a) returns false.
  4. Final Answer:

    bool(true)
    bool(true)
    bool(false)
    -> Option D
  5. Quick Check:

    isset true, empty true for 0, is_null false [OK]
Hint: 0 is set and empty, but not null [OK]
Common Mistakes:
  • Thinking isset returns false for 0
  • Confusing empty with is_null
  • Assuming 0 is null
4. Consider the following PHP code snippet:
$var = null;
if (empty($var)) {
echo 'Empty';
} else {
echo 'Not Empty';
}

Why does this code print "Empty"?
medium
A. Because empty returns false for null values.
B. Because empty only checks if variable is set.
C. Because empty returns true for null values.
D. Because empty throws an error for null values.

Solution

  1. Step 1: Understand empty behavior with null

    empty returns true if the variable is not set or its value is considered empty, including null.
  2. Step 2: Analyze the code output

    Since $var is null, empty($var) returns true, so "Empty" is printed.
  3. Final Answer:

    Because empty returns true for null values. -> Option C
  4. Quick Check:

    empty true for null [OK]
Hint: empty true for null or unset variables [OK]
Common Mistakes:
  • Thinking empty only checks if variable is set
  • Assuming empty returns false for null
  • Expecting error on null with empty
5. You want to check if a variable $x exists and is not null, but also want to treat the value 0 as valid (not empty). Which combination of PHP functions correctly achieves this?
hard
A. if (isset($x) && $x !== null) { ... }
B. if (isset($x) && !empty($x)) { ... }
C. if (!is_null($x) && !empty($x)) { ... }
D. if (!empty($x) || isset($x)) { ... }

Solution

  1. Step 1: Understand the requirement

    We want to check if $x exists and is not null, but 0 should be valid (not treated as empty).
  2. Step 2: Analyze each option

    if (isset($x) && !empty($x)) { ... } fails because empty(0) is true, so 0 would be excluded. if (isset($x) && $x !== null) { ... } checks if $x is set and not null, allowing 0. if (!is_null($x) && !empty($x)) { ... } excludes 0 because of empty($x). if (!empty($x) || isset($x)) { ... } uses OR, which can be true even if $x is empty or null.
  3. Final Answer:

    if (isset($x) && $x !== null) { ... } -> Option A
  4. Quick Check:

    Use isset and strict null check to allow 0 [OK]
Hint: Use isset and !== null to allow zero values [OK]
Common Mistakes:
  • Using empty excludes zero values
  • Using OR instead of AND
  • Confusing is_null with isset
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard