Loops and PHP Decision Making - Looping
(Page 3 of 5 )
Now that you've changed the flow of your PHP program based on comparisons, you need to learn that if you want to repeat a task until a comparison is FALSE, you'll need to use looping. Each time the code in the loop executes, it is called an iteration. This is useful for many common tasks, such as displaying the results of a query by looping through the returned rows. PHP provides the while, for, and do ... while constructs to perform loops.
Each of the loop constructs requires two basic pieces of information. First, the condition to stop looping is defined just like the comparison in an if statement. Second, the code to perform is also required and specified either on a single line or within curly braces. A logical error would be to omit the code from a loop that relies on the code executed to cause the loop to stop, causing an infinite loop.
The code is executed as long as the expression evaluates to TRUE. To avoid an infinite loop, which would loop forever, your code should have the expressions eventually become FALSE. When this happens, the loop stops, and execution continues with the next line of code, following the logical loop.
while Loops
The while loop takes the expression followed by the code to execute. Figure 4-3 illustrates how a while loop processes.

Figure 4-3. How a while loop executes
The syntax for a while loop is:
while (expression)
{
code to execute;
}
An example is shown in Example 4-14.
Example 4-14. A sample while loop that counts to 10
<?php
$num = 1;
while ($num <= 10){
print "Number is $num<br />";
$num++;
}
print 'Done.';
?>
Example 4-14 produces the following:
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Number is 10
Done.
Before the loop begins, the variable $num is set to 1. This is called initializing a counter variable. Each time the code block executes, it increases the value in $num by 1 with the statement $num++;. After 10 iterations, the evaluation $num <= 10 becomes FALSE, then the loop stops and it prints Done. Be sure to increase the $num var, as the while loop depends on it.
Be careful not to create an infinite loop. It has the undesirable effect of not returning your page and taking a lot of processing time on the web server.
do ... while Loops
The do ... while loop takes an expression such as a while statement but places it at the end. The syntax is:
do {
code to execute;
} while (expression);
This loop is useful when you want to execute a block of code at least once regardless of the expression value. For example, let's count to 10 with this loop, as shown in Example 4-15.
Example 4-15. Counting to 10 with do ... while
<?php
$num = 1;
do {
echo "Number is ".$num."<br />";
$num++;
} while ($num <= 10);
echo "Done.";
?>
Example 4-15 produces the same results as Example 4-14; if you change the value of $num to 11, the loop processes differently:
<?php
$num = 11;
do {
echo $num;
$num++;
} while ($num <= 10);
?>
This produces:
11
The code in the loop displays 11 because the loop always executes at least once. Following the pass, while evaluates to FALSE, causing execution to drop out of the do ... while loop.
Next: for Loops >>
More Programming Basics Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Learning PHP and MySQL, Second Edition, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Check it out today at your favorite bookstore. Buy this book now.
|
|