Sometimes, we want to determine every Nth iteration of a loop with PHP.
In this article, we’ll look at how to determine every Nth iteration of a loop with PHP.
How to determine every Nth iteration of a loop with PHP?
To determine every Nth iteration of a loop with PHP, we can check the index variable value with the modulo operator.
For instance, we write
$x = 3;
for ($i = 0; $i < 10; $i++) {
if ($i % $x == 0) {
// ...
}
}
to check if loop variable $i
is evenly divisible by $x
with
$i % $x == 0
Conclusion
To determine every Nth iteration of a loop with PHP, we can check the index variable value with the modulo operator.