From the course: PHP for Web Designers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Repeating output at specific intervals in a loop

Repeating output at specific intervals in a loop - PHP Tutorial

From the course: PHP for Web Designers

Start my 1-month free trial

Repeating output at specific intervals in a loop

So now we know how modulo division can be used to identify every fourth item in a series. We can fix the loop, in mixed.php. To keep track of the number of times the loop has run, we need to initialize a counter variable, outside the loop, and set its value to zero. So, the loop begins on line 57. We need to add our counter variable before that loop. So, let's just add in a few lines. And counter variables in PHP, by tradition, are usually $i. So, we'll set that to zero. And then, when the loop begins, we need to have an if decision making block. So if, and then the condition will be with a i, is equally divisible by four. So using modulo division, and to make sure that it is actually zero and not a false result we need three equal signs, followed by zero, and then we need an opening curly brace for our block. Then these two tags will be displayed, and we need the closing curly brace for that conditional block. That goes there. So the first time that the loop runs, i will be zero…

Contents