Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - Loops
What will be the output of this PHP code?
$sum = 0;
for ($i = 1; $i <= 3; $i++) {
    $sum += $i;
}
echo $sum;
A6
B123
C3
D0
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop iterations

    The loop runs with $i = 1, 2, 3. Each time, $sum adds $i: 0+1=1, 1+2=3, 3+3=6.
  2. Step 2: Calculate final sum

    After the loop, $sum is 6, which is printed by echo.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Sum 1+2+3 = 6 [OK]
Quick Trick: Add loop variable each iteration to sum [OK]
Common Mistakes:
  • Concatenating numbers instead of adding
  • Starting loop at 0 instead of 1
  • Using wrong loop condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes