for is a loop. It will loop through a block of coding for a specified amount of times. The WHILE loop on the previous page used a counter method (control) to determine its looping times. The for loop has it's counter control in the condition area.
initial expression is assigning a value to start with (usually the start of a counter).
condition test tests the current status to determin if the loop should happen or not.
closing expression affects the current status (usually increasing or decreasing).
An Example :
Results :
What's happening :
1. The variable $test starts with a 0 value.
2. The condition test checks if $test has a value less than 10.
3. If the value is true, do the loop. If false, skip out of the loop.
4. After each loop is completed, increase the variable $test by one.
5. Return to step 2 and continue until the condition test becomes false.
for (initial expression; condition test; closing expression){
block of coding here;
}
block of coding here;
}
initial expression is assigning a value to start with (usually the start of a counter).
condition test tests the current status to determin if the loop should happen or not.
closing expression affects the current status (usually increasing or decreasing).
An Example :
<?php
for ($test=0; $test<10; $test++){
echo "$test is less than 10. <br />";
}
?>
for ($test=0; $test<10; $test++){
echo "$test is less than 10. <br />";
}
?>
Results :
0 is less than 10.
1 is less than 10.
2 is less than 10.
3 is less than 10.
4 is less than 10.
5 is less than 10.
6 is less than 10.
7 is less than 10.
8 is less than 10.
9 is less than 10.
1 is less than 10.
2 is less than 10.
3 is less than 10.
4 is less than 10.
5 is less than 10.
6 is less than 10.
7 is less than 10.
8 is less than 10.
9 is less than 10.
What's happening :
1. The variable $test starts with a 0 value.
2. The condition test checks if $test has a value less than 10.
3. If the value is true, do the loop. If false, skip out of the loop.
4. After each loop is completed, increase the variable $test by one.
5. Return to step 2 and continue until the condition test becomes false.

