Bird
0
0

Consider the following PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Type Handling
Consider the following PHP code snippet:
$var = null;
if (empty($var)) {
echo 'Empty';
} else {
echo 'Not Empty';
}

Why does this code print "Empty"?
ABecause <code>empty</code> returns false for null values.
BBecause <code>empty</code> only checks if variable is set.
CBecause <code>empty</code> returns true for null values.
DBecause <code>empty</code> throws an error for null values.
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes