Bird
0
0

What will this PHP code output?

medium📝 Predict Output Q5 of 15
PHP - Loops
What will this PHP code output?
for ($i = 5; $i > 0; $i -= 2) {
    echo $i . " ";
}
A5 3 1 0
B5 3 1
C5 4 3 2 1
D5 2 0
Step-by-Step Solution
Solution:
  1. Step 1: Understand loop control

    Starts at 5, decreases by 2 each time, continues while $i > 0.
  2. Step 2: Trace values of $i

    Values: 5 (print), 3 (print), 1 (print), next $i = -1 stops loop.
  3. Final Answer:

    5 3 1 -> Option B
  4. Quick Check:

    Decrement by 2, stop when <= 0 = C [OK]
Quick Trick: Loop stops when condition fails before printing [OK]
Common Mistakes:
  • Including 0 in output
  • Assuming decrement by 1 instead of 2

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes