HTML5 and CSS3 All-in-One For Dummies
Book image
Explore Book Buy On Amazon

The while loop is the primary way, other than a for loop, of repeating code in PHP. As an HTML5 and CSS3 programmer, there may be times when you choose one over the other, but it is important to understand the differences between the two.

image0.jpg

while loops are much like for loops. They require the same thought:

  • A sentry variable: This special variable controls access to the loop. Unlike the int usually used in for loops, the sentry of a while loop can be any type.

  • Initialization: Set the initial value of the sentry variable before the loop begins. Do not rely on default settings (because you don't know what they will be). Instead, set this value yourself.

  • A condition: The while statement requires a condition. This condition controls access to the loop. As long as the condition is true, the loop continues. As soon as the condition is evaluated as false, the loop exits.

  • A modifier: You must somehow modify the value of the sentry variable. It's important that the modification statement happen somewhere inside the loop. In a for loop, you almost always add or subtract to modify a variable. In a while loop, any kind of assignment statement can be used to modify the variable.

for loops are a little safer than while loops because the structure of the loop requires you to think about initialization, condition, and modification. All three features are built into the for statement. The while statement requires only the condition. This might make you think that you don't need the other parts, but that would be dangerous.

In any kind of loop, you need to initialize the sentry variable and modify its value. With the while loop, you're responsible for adding these features yourself. Failure to do so will cause endless loops, or loops that never happen.

Take a look at the following code for the while.php program to see how it works:

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>while.php</title>
 <style type="text/css">
 img {
  height: 40px;
  width: 50px;
 }
 </style>
</head>
<body>
 <h1>Dice Rolling Game 2</h1>
 <p>Welcome to the dice rolling game. See how many rolls it takes to get a six!</p>
 <div id = "output">
 <?php
 $userNumber = 999;
 $counter = 0;
 while ($userNumber != 6){
 $userNumber = rand(1,6);
 print <<< HERE
  <img src = "images/dado_$userNumber.png"
   alt = "$userNumber"
   height = "100px"
   width = "100px" />
HERE;
 $counter++;
 }
 print "<p>It took $counter tries to get a six.</p>";
 ?>
 </div>
 <p><a href="while.php">Try Again!</a></p>
</body>
</html>

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 is the sentry variable. The initialization needs to guarantee that the loop runs exactly once. Because the condition will be ($userNumber!=6), you 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 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, if you want the loop to quit when $userNumber is equal to 6, then you’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.

About This Article

This article is from the book:

About the book author:

Andy Harris taught himself programming because it was fun. Today he teaches computer science, game development, and web programming at the university level; is a technology consultant for the state of Indiana; has helped people with disabilities to form their own web development companies; and works with families who wish to teach computing at home.

This article can be found in the category: