Bird
0
0

Why does the expression "0" == false evaluate to true in PHP, but "false" == false evaluates to false?

hard📝 Conceptual Q10 of 15
PHP - Variables and Data Types
Why does the expression "0" == false evaluate to true in PHP, but "false" == false evaluates to false?
ABecause "0" converts to integer 0, matching false; "false" is a non-empty string, so converts to true
BBecause PHP treats all strings as true except "false"
CBecause "false" is a reserved keyword and cannot be compared
DBecause "0" is an empty string, but "false" is not
Step-by-Step Solution
Solution:
  1. Step 1: Understand PHP type juggling in loose comparison

    When comparing string to boolean, PHP converts string to number.
  2. Step 2: Analyze conversions

    "0" converts to 0, which equals false; "false" is non-numeric non-empty, so does not equal false.
  3. Final Answer:

    "0" converts to 0 matching false; "false" is non-empty string, so comparison fails. -> Option A
  4. Quick Check:

    String "0" equals false, string "false" does not [OK]
Quick Trick: String "0" loosely equals false, string "false" does not [OK]
Common Mistakes:
  • Assuming all strings equal false
  • Ignoring PHP type juggling rules
  • Confusing string content with boolean value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes