One of the most common programming functions we need to use in PHP is the simple FOR() loop, which repeats a particular section of code several times. You can make it repeat the code of fixed number of times, or just keep looping through until a particular condition is met. Here is the basic format:

for ( initialize a counter; conditional statement; increment a counter)
{
do this code;
}

This loop, for example, will loop through nine times, with $x value set to 1, 2, 3, 4, 5, 6, 7, 8 and 9. The $x=1 sets $x initially to 1. The $x++ increments $x after each time through the loop. $x<10 checks at the beginning of each loop, and if true, executes the code again. When no longer true (i.e. $x is equal to 10) the execution continues after the last curly bracket of the loop.

for ($x=1; $x<10; $x++)
{
do this code;
}

So, if you want to meet some arbitrary condition that is changed within the code inside the loop, you just change $x>10 to a different variable and value, say $y>0 — but be careful! It is easy to get into an ‘infinite loop’ if your conditional statement is always true. With PHP, that means the code will continue to run until you reach the maximum time allotted to any one routine, and then will end with an error message — usually not a good thing!