Bird
0
0

You want to compare two variables in PHP: $a = '0' (string) and $b = false (boolean). Which comparison returns true when using type coercion, and which returns false when checking type strictly?

hard📝 Application Q15 of 15
PHP - Type Handling
You want to compare two variables in PHP: $a = '0' (string) and $b = false (boolean). Which comparison returns true when using type coercion, and which returns false when checking type strictly?

Choose the correct pair:
A<code>$a == $b</code> is true; <code>$a === $b</code> is false
B<code>$a == $b</code> is false; <code>$a === $b</code> is true
C<code>$a == $b</code> is true; <code>$a === $b</code> is true
D<code>$a == $b</code> is false; <code>$a === $b</code> is false
Step-by-Step Solution
Solution:
  1. Step 1: Understand loose (==) vs strict (===) comparison

    Loose comparison converts types before comparing; strict compares value and type exactly.
  2. Step 2: Evaluate comparisons

    Loose: '0' converts to 0, false converts to 0, so 0 == 0 is true. Strict: '0' is string, false is boolean, types differ, so false.
  3. Final Answer:

    $a == $b is true; $a === $b is false -> Option A
  4. Quick Check:

    '0' == false is true; '0' === false is false [OK]
Quick Trick: Use === to check type and value, == allows type coercion [OK]
Common Mistakes:
  • Assuming == checks type strictly
  • Thinking '0' and false are always different
  • Confusing loose and strict equality

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes