Bird
0
0

Find the issue in this PHP snippet:

medium📝 Debug Q7 of 15
PHP - Type Handling
Find the issue in this PHP snippet:
$x = '20';
$y = true;
$z = $x . $y;
echo $z;
AError due to type mismatch
BOutputs '201'
COutputs '20true'
DOutputs 21
Step-by-Step Solution
Solution:
  1. Step 1: Understand concatenation with '.' operator

    The '.' operator converts both operands to strings and joins them.
  2. Step 2: Convert true to string

    Boolean true converts to '1' when cast to string.
  3. Step 3: Concatenate '20' and '1'

    Result is '201'.
  4. Final Answer:

    Outputs '201' -> Option B
  5. Quick Check:

    Boolean true as string = '1' in concatenation [OK]
Quick Trick: '.' concatenates strings; true becomes '1' as string [OK]
Common Mistakes:
  • Expecting '20true'
  • Assuming error on boolean concat
  • Thinking '+' concatenates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes