This forum is now read-only. Please use our new forums at

For Loops in PHP Forum

View Course » View Exercise

1234 points
Submitted by
about 5 years ago

Why $i > 4 and not &i < 4 ?

So the second variable is stating when the loop should stop, so, if i'm counting from 50 to 5 why should i say that $i > 4 (So is major than 4) and not $i <4 (to stop when is minor than 4, which seem more logical), i'm sure i'm missing something but i can't get what.

Thanks in advance for any help.

5 votes


I think you're confusing the 2nd element as an instruction for when to STOP counting. It's actually an instruction to CONTINUE AS LONG AS... (in this case $i > 4)

So... if the variable is Greater than 4 ( $i > 4 )... keep counting down.

400 points
Submitted by
about 5 years ago


E741f0115863cdf7096476c30d02a18c?s=140&d=retro Kelly O'Hara about 5 years ago

Absolutely right... Think of it as for every time you're ready to start the next loop, you are checking whether you should go through or finally skip out. It looks at the second part and basically solves the expression....

In this case:

First pass, $i is equal to 50 and we look at the second element and determine if we should still run.

  • In the valid case, $i > 4 evaluates to "True", so yes, the loop should continue.
  • In your opposing case $i < 4, the very first pass (with $i = 50) it will evaluate to "False" and say "we're done, skip the loop". In that case, it won't even run in the loop, it'll just skip over it.

An alternative that should work as well (only in specific iterative cases where the value matches a valid value in the iterator) would be to check for the value, like $i != 5. Then that would work in either direction, just as long as $i isn't 5. But then you would lose that iteration, so you would look for the next valid value in the group, $i != 0. Now that would work, as the last valid value is 5. It's not the brightest (iterator step changes, your conditions have to be modified to match) or most effective manner to write it, but it does work and might help to understand the for looping better.

54f57c44e12b592ffb8e141b7c3392ca?s=140&d=retro eleclipse about 5 years ago

That's right thanks!

Funny thing is that i did id right before and i even got it before, i must have been out of my mind.

0 votes


You can also check the other questions regarding the for loops in the forum, there is a solution inside for the countdown from 50 to 5.

312 points
Submitted by
Plamena Kirilova
about 5 years ago