Bird
0
0

Which of the following PHP expressions correctly adds a boolean true to an integer 2 and returns 3?

easy📝 Syntax Q12 of 15
PHP - Type Handling
Which of the following PHP expressions correctly adds a boolean true to an integer 2 and returns 3?
A<code>(int)'true' + 2</code>
B<code>true + 2</code>
C<code>true . 2</code>
D<code>'true' + 2</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand boolean to integer conversion

    In PHP, boolean true converts to integer 1 in arithmetic operations.
  2. Step 2: Evaluate each option

    (int)'true' + 2: (int)'true' is 0 + 2 = 2 (wrong). 'true' + 2: 'true' converts to 0 + 2 = 2 (wrong). true . 2: concatenation '12' (wrong). true + 2: 1 + 2 = 3 (correct).
  3. Final Answer:

    true + 2 -> Option B
  4. Quick Check:

    true + 2 = 3 [OK]
Quick Trick: Boolean true acts as 1 in math operations [OK]
Common Mistakes:
  • Confusing string 'true' with boolean true
  • Using concatenation instead of addition
  • Not knowing boolean converts to 1 or 0

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes