JohnA JohnA - 2 months ago 13
PHP Question

PHP Loop do action for each 10, 20, 30 etc

In php i have loop e.g.

for ($i = 0; $i <= 1000; $i++) {
if ($i = 10 || $i == 20 || $i == 30 || $i == 40 || $i == 50 || $i == 60) {
echo $i;
}
}


imagine i need to echo $i every
10,20,30,40,50,60,..970,980,990
there should be way to not write 100 conditions in
if
statement. Is there some logical way to see if
$i
increased by 10 then do something like:

if ($i == $i+10) {
...
}


P.S. if possible i dont want to introduce another variable to count i need to find solution with using only
$i

Answer

Try:

if ($i % 10 == 0) 

That will trigger whenever your index is exactly divisible by 10.