PHP For
The for loop is used when you need to execute the same code a predetermined number of times.
For more PHP loop examples, see:
PHP Foreach Loop
PHP Do While Loop
PHP While Loop
For Loop Syntax
for ( expression 1, expression 2, expression 3){
do this;
}
The for loop will first execute “expression 1″. Once it has done that “expression 2″ will be evaluated, and if it is TRUE, then “do this” is executed. The real loop begins now as “expression 3″ is then executed, “expression 2″ is evaluated, “do this” is executed…
The cycle of [expression 3, expression 2, do this] will continue until “expression 2″ is no longer TRUE.
For Loop Example
Let me show you a very simple example, then I will explain it in detail:
{
echo “Count: ” . $i . “<br />”;
}
The above code would display:
Count: 0
Count: 1
Count: 2
Count: 3
Again, the for loop continues until “expression 2″ is no longer true. Let’s break this down:
In the above code, we used $i as an integer beginning with 0. Our “expression 2″ specified that as long as the integer ($i) was less than or equal to three, echo “Count: $i” where $i is the current integer. Lastly, “expression 3″ told us to +1 to whatever number we currently have.
If you’re a little lost, just follow our loop syntax as described above.
The for loop will continue to cycle, starting with the number 0 and repeating, each time adding 1, until the number was equal or greater than 3, then it would stop because our “expression 2″ would then be false.
For more PHP loop examples, see:




