Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Loops

What will be the output of this PHP code?

$count = 5;
while ($count > 0) {
    echo $count . ',';
    $count -= 2;
}
A5,3,1,-1,
B5,3,1,
C5,4,3,2,1,
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand loop condition and decrement

    Loop runs while $count > 0, decreasing by 2 each time.
  2. Step 2: Trace values printed

    Prints 5, then 3, then 1. Next decrement makes $count = -1, loop stops.
  3. Final Answer:

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

    Loop prints odd numbers down to 1 [OK]
Quick Trick: Decrement inside loop affects iterations [OK]
Common Mistakes:
  • Including negative values
  • Assuming decrement by 1
  • No output if condition misunderstood

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes