Bird
0
0

Given $var = '123abc';, which code snippet correctly converts $var to an integer and then to a boolean, preserving the final boolean value?

hard📝 Application Q9 of 15
PHP - Type Handling
Given $var = '123abc';, which code snippet correctly converts $var to an integer and then to a boolean, preserving the final boolean value?
Asettype($var, 'float'); settype($var, 'bool');
Bsettype($var, 'bool'); settype($var, 'int');
Csettype($var, 'string'); settype($var, 'bool');
Dsettype($var, 'int'); settype($var, 'bool');
Step-by-Step Solution
Solution:
  1. Step 1: Convert string to integer first

    Using settype($var, 'int') converts '123abc' to integer 123.
  2. Step 2: Convert integer to boolean

    Then settype($var, 'bool') converts 123 to boolean true (non-zero).
  3. Final Answer:

    settype($var, 'int'); settype($var, 'bool'); -> Option D
  4. Quick Check:

    Convert string to int, then int to bool for correct boolean value [OK]
Quick Trick: Convert to int before bool to preserve numeric truthiness [OK]
Common Mistakes:
  • Converting to bool before int loses numeric info
  • Using string conversion before bool
  • Using float conversion unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes