Loops add control to scripts so that statements can be repeatedly executed as long as a conditional expression remains true. There are four loop statements in PHP: while, do...while, for, and foreach. The first three are general-purpose loop constructs, while the foreach is used exclusively with arrays and is discussed in the next chapter.
The while loop is the simplest looping structure but sometimes the least compact to use. The while loop repeats one or more statements?the loop body?as long as a condition remains true. The condition is checked first, then the loop body is executed. So, the loop never executes if the condition isn't initially true. Just as with the if statement, more than one statement can be placed in braces to form the loop body.
The following fragment illustrates the while statement by printing out the integers from 1 to 10 separated by a space character:
$counter = 1; while ($counter < 11) { print $counter . " "; $counter++; }
The difference between while and do...while is the point at which the condition is checked. In do...while, the condition is checked after the loop body is executed. As long as the condition remains true, the loop body is repeated.
You can emulate the functionality of the previous while example as follows:
$counter = 1; do { print $counter . " "; $counter++; } while ($counter < 11);
The contrast between while and do...while can be seen in the following example:
$counter = 100; do { print $counter . " "; $counter++; } while ($counter < 11);
This example outputs 100, because the body of the loop is executed once before the condition is evaluated as false.
The do...while loop is the least frequently used loop construct, probably because executing a loop body once when a condition is false is an unusual requirement.
The for loop is the most complicated of the loop constructs, but it also leads to the most compact code.
Consider this fragment that implements the example used to illustrate while and do...while:
for($counter=1; $counter<11; $counter++) { print $counter; print " "; }
The for loop statement has three parts separated by semicolons, and all parts are optional:
Statements that are executed once, before the loop body is executed.
The conditional expression that is evaluated before each execution of the loop body. If the conditional expression evaluates as false, the loop body is not executed.
Statements that are executed each time after the loop body is executed.
The previous code fragment has the same output as our while and do...while loop count-to-10 examples. $counter=1 is an initial statement that is executed only once, before the loop body is executed. The loop condition is $counter<11, and this is checked each time before the loop body is executed; when the condition is no longer true (when $counter reaches 11) the loop is terminated. The end-loop statement $counter++ is executed each time after the loop body statements.
Our example is a typical for loop. The initial statement sets up a counter, the loop condition checks the counter, and the end-loop statement increments the counter. Most for loops used in PHP scripts have this format.
Conditions can be as complex as required, as in an if statement. Moreover, several initial and end-loop statements can be separated by commas. This allows for complexity:
for($x=0,$y=0; $x<10&&$y<$z; $x++,$y+=2)
However, complex for loops can lead to confusing code.
To break out of a loop early?before the loop condition becomes false?the break statement is useful. This example illustrates the idea:
for($x=0; $x<100; $x++) { if ($x > $y) break; print $x; }
If $x reaches 100, the loop terminates normally. However, if $x is (or becomes) greater than $y, the loop is terminated early, and program execution continues after the closing brace of the loop body. The break statement can be used with all loop types.
To start again from the top of the loop without completing all the statements in the loop body, use the continue statement. Consider this example:
$x = 1; while($x<100) { print $x; $x++; if ($x > $y) continue; print $y; }
The example prints and increments $x each time the loop body is executed. If $x is greater than $y, the sequence starts again with the print $x; statement (and $x keeps the value that was assigned to it during the loop). Otherwise, $y is printed and the loop begins again normally. Like the break statement, continue can be used with any loop type.
The use of break and continue statements to change loop behavior makes code harder to understand and should be avoided.