PHP for Loops. Free online PHP and MySQL Web Database Programming Tutorial...
|Lessons||PHP for Loop|
for (init; condition; increment)
code to be executed;
The parameters of the for statement above are explained as under:
•init: The is mostly used to set the loop counter, but could be any code to be executed once at the beginning of the loop statement.
•condition: This is evaluated at the beginning of every loop iteration. If the condition evaluates to TRUE, the loop continues and the code executes. If it evaluates to FALSE, the execution of the loop ends.
•increment: This is mostly used to increment a counter, but can be any code to be executed at the end of each loop iteration.
Any or all of the parameters can be omitted or there may be multiple expressions separated by commas. Please note that if we have multiple statements for the condition parameter, they all are evaluated but the result is taken from the last part. Leaving the condition parameter as empty means that the loop should be run indefinitely. Be careful when doing this, we must have some other sort of algorithm to end the loop e.g. by using a break statement inside the loop body.
The following example prints the text "Hello World!" five times:
for ($i=1; $i<=5; $i++)
echo "Hello World!<br />";
Next: PHP Foreach Loop