PHP, like most languages, provides another kind of looping structure that is even more flexible than the for loop. The while loop can be used when you know how many times something will happen, just like the for loop. Figure 4.6 shows how a while loop can work much like a for loop:
The code for the while.php program is much like the for loop demo, but you can see that the loop is a little bit simpler:
<html> <head> <title> A simple While Loop </title> </head> <body> <h1>A simple while loop</h1> <? $i = 1; while ($i <= 10){ print "$i <br>\n"; $i++; } // end while ?> </body> </html>
The while loop requires only one parameter, which is a condition. The loop will continue as long as the condition is evaluated as true. As soon as the condition is evaluated as false, the loop will exit. This particular program starts by initializing the variable $i, then checking to see if it's greater than or equal to ten in the while statement. Inside the loop body, the program prints out the current value of $i and increments $i.
The flexibility of the while construct gives it power, but with that power comes some potential for problems. while loops are easy to build, but a loop that does not work properly can cause a lot of problems. It's possible that the code in the loop will never execute at all. Even worse, you might have some sort of logical error that causes the loop to continue indefinitely. As an example, look at the following code:
<html> <head> <title> A bad While Loop </title> </head> <body> <h1>A bad while loop</h1> <? $i = 1; while ($i <= 10){ print "$i <br>\n"; $j++; } // end while ?> </body> </html>
TRAP |
The badWhile.php program is intended to show what happens when you have an endless loop in your code. If you run this program, it's possible it will cause a temporary slowdown of your Web server. Be sure your server is configured to stop a PHP process when the user presses the Stop button on the browser. (This is a default setting on most installations of PHP.) |
The badWhile.php program has a subtle but deadly error. Look carefully at the source code and see if you can spot it. The code is just like the first while program, except instead of incrementing $i, I incremented $j. The variable $j has nothing to do with $i, and $i never changes. The loop keeps going on forever, because it cannot end until $i is greater than or equal to ten, which will never happen. This program is an example of the classic "endless loop." Every programmer alive has written them accidentally, and you will too.
TRICK |
Usually the culprit of an endless loop is a sloppy variable name, spelling, or capitalization. If you use a variable like $myCounter as the sentry variable, but then increment $MyCounter, PHP will keep track of two entirely different variables, and your program won't work correctly. This is another reason to be consistent on your variable naming and capitalization conventions. |
Fortunately, there are some guidelines for building a loop that behaves as you wish. Even better, you've already learned most of the important ideas, because these fundamental concepts are built in to the structure of the for loop. When you write a while loop, you are responsible for these three things:
Creating a sentry variable
Building a condition
Ensuring the loop can exit
I'll discuss each of these ideas in the following sections.
If your loop will be based on the value of a variable (there are some other alternatives), make sure you identify that variable, make sure it has appropriate scope, and make sure it has a reasonable starting value. You might also check that value to ensure the loop runs at least one time (at least if that's your intent). Creating a variable is much like the initialization stage of a for construct.
Your condition will usually compare a variable and a value. Make sure you have a condition that can be met, and that can be broken. The hard part is not building the loop, but ensuring the program gets out of the loop at the correct time. This condition is much like the condition in the for loop.
There must be some trigger that changes the sentry variable so the loop can exit. This code must exist inside the code body. Be sure it is possible for the sentry variable to achieve the value necessary to exit the loop by making the condition false.