HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [215]
Take a look at the following code for the while.php program to see how it works:
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
Dice Rolling Game 2
Welcome to the dice rolling game. See how many rolls it takes to get a six!
$userNumber = 999;
$counter = 0;
while ($userNumber != 6){
$userNumber = rand(1,6);
print “
”;
$counter++;
}
print “
It took $counter tries to get a six.
“;?>
Can I use a debugger for PHP?
In Book IV, you can see how to use the firebug debugger to check your code. This is especially handy for the logic errors that tend to occur when you’re writing while loops. It would be great if there was a similar facility for PHP code. Unfortunately, PHP debuggers are relatively rare and can be difficult to install and use. That’s because PHP is not an interactive language, but it processes code in batch mode on the server. Firebug is a client-side application, and it doesn’t ever see the PHP code. The best way to debug PHP is with good-old print statements. If something doesn’t work correctly, print out the sentry variable before, inside, and after the loop to see whether you can find the pattern. One reason why people are switching to AJAX (see Book VII) is that much of the logic is done on the client side, where it’s easier to debug.
This example illustrates how subtle while loops can be. All the key elements are there, but they don’t all look like part of the while loop.
1. Initialize $userNumber.
For this loop, $userNumber will be the sentry variable. The initialization needs to guarantee that the loop runs exactly once. Because the condition will be ($userNumber != 6), I need to give $userNumber a value that clearly isn’t 6. 999 will do the job, and it’s wild enough to be clearly out of range. Although the initialization step appears in the code before the while loop, it’s often best to start with your condition and then back up a line to initialize because the initialization step depends on the condition.
2. Set up the condition.
Think about what should cause the loop to continue or quit. Remember that the condition explains when the loop continues. It’s often easier to think about what causes the loop to exit. That’s fine; just reverse it. For example, I want the loop to quit when $userNumber is equal to 6, so I’ll have it continue as long as $userNumber != 6.
3. Modify the sentry.
This one is tricky. In this particular example, modify the sentry variable by getting a new random number: $userNumber = rand(1,6). Often in a while loop, the modification step is intrinsic to the problem you’re solving. Sometimes you get the new value from the user, sometimes you get it from a file or database, or sometimes you just add (just like a for loop). The key here is to ensure you have a statement that modifies the sentry variable and that the condition can trigger. For example, using $userNumber = rand(1,5) would result in an endless loop because $userNumber could never be 6.
while loops can cause a lot of problems because they may cause logic errors. That is, the syntax (structure and spelling of the code) may be fine, but the program still doesn’t operate properly. Almost always, the problem can be resolved by thinking about those three parts of a well-behaved loop: Initialize the sentry, create a meaningful condition, and modify the sentry appropriately. See Book IV, Chapter 3 for more on while loops.
Chapter 4: Working with Arrays
In This Chapter
Creating one-dimensional