## How to Do Something Every Nth Iteration in a PHP Loop

If you’re a PHP coder, you are sure to come across a scenario from time to time where you have a PHP loop and you need to execute specific code every other time the loop iterates or every four times the loop iterates etc.

## Using the Modulus Operator

The key to making something happen every nth time in a loop is the modulus operator or %.

A common scenario where you would need to do something in a loop every fourth or fifth iteration only is when you are printing a series of HTML elements. Let’s pretend we have 12 items in an array and we want to print them in groups of four per line. While printing the array you will need to print the <br /> tag every four iterations if you want four items on each line.  The code inside your PHP loop would look like this:

if(\$i % 4 == 0){echo “<br />”;}

Now let’s put this to the test with some test code using the above scenario. Below I fill an array with 12 words and print them out in a loop four per line:

<?php
\$words = array(‘one’,’two’,’three’,’four’,’five’,’six’,’seven’,’eight’,’nine’,’ten’,’eleven’,’twelve’);
\$noof = count(\$words);
\$i=0;
for(\$ii=0;\$ii<\$noof;\$ii++){
\$i++;
echo \$words[\$ii];
echo ” “;
if(\$i % 4 == 0){echo “<br />”;}
}//end for loop
?>

Noticed I used \$ii in the for loop and set a second variable, \$i for using with the modulus line. This is because \$ii will start at zero and we need to start at 1 in this case. The results of the above code would be:

one two three four
five six seven eight
nine ten eleven twelve

Today however, I came across a more complex scenario where I was able to also use the modulus operator to save the day. This time I needed to print items from an array inside of HTML list elements. Lets modify the above scenario using lists instead of line breaks to display the array data:

<?php
\$words = array(‘one’,’two’,’three’,’four’,’five’,’six’,’seven’,’eight’,’nine’,’ten’,’eleven’,’twelve’);
\$noof = count(\$words);
\$i=0;
for(\$ii=0;\$ii<\$noof;\$ii++){
\$i++;
if(\$i % 4 == 1)echo “<ul>”;
echo “<li>”;
echo \$words[\$ii];
echo “</li>”;
if(\$i % 4 == 0)echo “</ul>”;
}//end for loop
?>

The output of the above PHP code would be:

1. one
2. two
3. three
4. four
1. five
2. six
3. seven
4. eight
1. nine
2. ten
3. eleven
4. twelve

The results were four separate ordered lists  as you can see. But how did we do this? Notice the difference in the modulus operator use for the opening <ol> tag and then for the closing </ol> tag.

The tricky part here is we can’t use if(\$i % 4 == 0) on the opening <ol> tag because if you think about it you don’t need <ol> printed on 4,8 and 12 which is what if(\$i % 4 == 0) would produce. In this case, we need to print an opening <ol> tag on iterations 1,5,9 and 13, so we try something else. Here is how I figured this problem out:

First a simple demo of the modulus operator as we first used it:

<?php
\$i=0;
for(\$ii=0;\$ii<20;\$ii++){
\$i++;
if(\$i % 4 == 0)echo “\$i<br />”;
}//end for loop
?>

The above code results in:

4
8
12
16
20

Which is fine for printing the end </ol> tags, but will not work for printing the opening <ol> tags as I explained earlier, so I tried this instead:

<?php
\$i=0;
for(\$ii=0;\$ii<20;\$ii++){
\$i++;
if(\$i % 4 == 1)echo “\$i<br />”;
}//end for loop
?>

This time the above code printed out:

1
5
9
13
17

So, if we used a one instead of a zero like if(\$i % 4 == 1), then it would execute the code in the if statement on iterations 1,5,9 and 13 just as we need! Just for fun, let’s see what happens if we use a two instead of zero this time like if(\$i % 4 == 2). For example:

<?php
\$i=0;
for(\$ii=0;\$ii<20;\$ii++){
\$i++;
if(\$i % 4 == 2)echo “\$i<br />”;
}//end for loop
?>

This time the results would be:

2
6
10
14
18

so using a 2 to compare 4 modulus with results in the code executing on iterations 2,6,10….etc, but notice in each example where we use 4 as the first number, all the numbers are increments of 4 and when we use zero as the last number we get 4,8,12…. When we use 1 we get 1,5,9…. Finally if we use 2 we get 2,6,10…. From what we have learned I think it’s safe to assume that we we used 3, the results would be 3,7,11,15….and so on.

## Summary

I hope this article sheds some light on how to use the Modulus operator with PHP. Feel free to experiment by using other numbers in place of where I used 4 each time too! For example, if you want to do something every 5 times instead, you would use something like if(\$i % 5 == 0). Have fun with it!