For loops are useful constructions to loop through a finite set of data, such as data in an array:
for($i = 0; $i < sizeof($array); $i++) { //do something with array[$i]; }
For loops require three things when they are defined:
Counter In the above example, $i = 0. You can pass in an already assigned variable or assign a value to the variable in the for statement.
The condition required to continue the for loop In the above example, while $i is less than the size of the $array, the for loop continues to be executed.
Statement to modify the counter on each pass of the loop In the above example, $i++ increments the counter after each loop.
Top |