Bird
0
0

Why does settype convert the string '0' to boolean false, but the string 'false' to boolean true?

hard📝 Conceptual Q10 of 15
PHP - Type Handling
Why does settype convert the string '0' to boolean false, but the string 'false' to boolean true?
ABecause '0' is considered falsy, but any non-empty string is truthy in PHP.
BBecause 'false' is a special keyword converted to false boolean.
CBecause settype treats strings starting with 'f' as true.
DBecause settype throws an error on 'false' string conversion.
Step-by-Step Solution
Solution:
  1. Step 1: Understand PHP boolean conversion rules

    In PHP, the string '0' is falsy, but any other non-empty string (including 'false') is truthy.
  2. Step 2: Apply to settype behavior

    settype converts '0' to false boolean, but 'false' remains true because it is a non-empty string.
  3. Final Answer:

    Because '0' is considered falsy, but any non-empty string is truthy in PHP. -> Option A
  4. Quick Check:

    Non-empty strings are true except '0' string [OK]
Quick Trick: Only '0' string is false; other strings are true in boolean context [OK]
Common Mistakes:
  • Assuming 'false' string converts to false boolean
  • Thinking settype parses string content for keywords
  • Believing settype throws errors on 'false' string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes