Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Loops
What will be the output of this PHP code?
$i = 1;
do {
  echo $i . ' ';
  $i++;
} while ($i <= 3);
A1 2
B2 3
C1 2 3
D3 2 1
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop iterations

    Initially, $i=1. Loop prints 1, then increments to 2. Condition $i<=3 is true, so loop continues.
  2. Step 2: Continue tracing

    Next, prints 2, increments to 3, condition true. Then prints 3, increments to 4, condition false, loop stops.
  3. Final Answer:

    1 2 3 -> Option C
  4. Quick Check:

    do-while runs 3 times printing 1 to 3 [OK]
Quick Trick: do-while runs while condition true after body [OK]
Common Mistakes:
  • Stopping loop too early
  • Printing incremented value instead of current
  • Confusing order of output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes