For loops offer a more restricted way of setting up code for repetitive execution. For loops begin with the keyword FOR and then a set of 3 statements: a variable set to an initial value to be used as the loop's counter, the conditional statement and then the increment in which the counter value will be adjusted after each execution. The code block is bracketed and follows the for statement.
<?php for ($i = 12; $i > 0; $i--) { echo "I'm eating a donut...<br />"; } echo "I ate them all!"; ?>
In this example, the $i variable serves the same purpose as our previous $donuts_remaining variable. It's initial value has been set to 12 and the code block will be processed until it's value is no longer greater than 0. Each time the block is executed, $i will be decreased by one.
For loops are more restricted in that the variable is intended only to act as a counter mechanism. It wouldn't be good practice to change it's value in within the code block! This is compared to the do-while and while loops where the value is intentionally changed in the code block to affect the loop's behavior.