PHP Looping

One of the great things about computers is that they can repeat calculating tasks quickly and tirelessly. Often you may want a program to repeat the same sequence of code again and again until something happens, such as a user inputting a value or reaching a natural end. PHP’s various loop structures provide the perfect way to do this. To picture how this works, take a look at Figure 4-4. It is much the same as the highway metaphor used to illustrate if statements, except that the detour also has a loop section that—once a vehicle has entered—can be exited only under the right program conditions.

PHP Looping
Figure 4-4. Imagining a loop as part of a program highway layout

 

There are 4 Loops in PHP:

  1. While loop
  2. do while loop
  3. for loop
  4. for each loop

while Loops

Let’s turn the digital car dashboard in Example 4-26 into a loop that continuously checks the fuel level as you drive, using a while loop (Example 4-28).

Example 4-28. A while loop
<?php
$fuel = 10;
while ($fuel > 1)
{
// Keep driving ...
echo "There's enough fuel";
}
?>

Actually, you might prefer to keep a green light lit rather than output text, but the point is that whatever positive indication you wish to make about the level of fuel is placed inside the while loop. By the way, if you try this example for yourself, note that it will keep printing the string until you click the Stop button in your browser. As with if statements, you will notice that curly braces are required to hold the statements inside the while statements unless there’s only one.
For another example of a while loop that displays the 12 times table, see Example 4-29.

Example 4-29. A while loop to print the 12 times table
<?php
 $count = 1;
 while ($count <= 12)
 {
 echo "$count times 12 is " . $count * 12 . "<br>";
 ++$count;
 }
?>

Here the variable $count is initialized to a value of 1, then a while loop is started with
the comparative expression $count <= 12. This loop will continue executing until the
variable is greater than 12. The output from this code is as follows:
1 times 12 is 12
2 times 12 is 24
3 times 12 is 36
and so on…

 

Inside the loop, a string is printed along with the value of $count multiplied by 12. For neatness, this is also followed with a <br> tag to force a new line. Then $count is incremented, ready for the final curly brace that tells PHP to return to the start of the loop. At this point, $count is again tested to see whether it is greater than 12. It isn’t, but it now has the value 2, and after another 11 times around the loop, it will have the value 13. When that happens, the code within the while loop is skipped and execution passes on to the code following the loop, which, in this case, is the end of the program.
If the ++$count statement (which could equally have been $count++) had not been there, this loop would be like the first one in this section. It would never end and only the result of 1 * 12 would be printed over and over.
But there is a much neater way this loop can be written, which I think you will like. Take a look at Example 4-30.

Example 4-30. A shortened version of Example 4-29
<?php
$count = 0;
while (++$count <= 12)
echo "$count times 12 is " . $count * 12 . "<br>";
?>

In this example, it was possible to remove the ++$count statement from inside the while loop and place it directly into the conditional expression of the loop. What now happens is that PHP encounters the variable $count at the start of each iteration of the loop and, noticing that it is prefaced with the increment operator, first increments the variable and only then compares it to the value 12. You can, therefore, see that $count now has to be initialized to 0, not 1, because it is incremented as soon as the loop is entered. If you keep the initialization at 1, only results between 2 and 12 will be output.

 

<< Prev Next >>

Loading