Looping by Robin Nixon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

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.

Imagining a loop as part of a program highway layout

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

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.

Note

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 will be skipped and execution will pass 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 have been like the first one in this section. It would never end, and only the result of 1 * 12 would be printed over and over.

There’s a much neater way that this loop can be written, though, 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.

do…while Loops

A slight variation to the while loop is the do...while loop, used when you want a block of code to be executed at least once and made conditional only after that. Example 4-31 shows a modified version of our code for the 12 times table using such a loop.

Example 4-31. A do…while loop for printing the times table for 12

<?php
$count = 1;
do
    echo "$count times 12 is " . $count * 12 . "<br />";
while (++$count <= 12);
?>

Notice that we are back to initializing $count to 1 (rather than 0), because the code is being executed immediately, without an opportunity to increment the variable. Other than that, though, the code looks pretty similar to Example 4-29.

Of course, if you have more than a single statement inside a do...while loop, remember to use curly braces, as in Example 4-32.

Example 4-32. Expanding Example 4-31 to use curly braces

<?php
$count = 1;
do {
    echo "$count times 12 is " . $count * 12;
    echo "<br />";
} while (++$count <= 12);
?>

for Loops

The final kind of loop statement, the for loop, is also the most powerful, as it combines the abilities to set up variables as you enter the loop, test for conditions while iterating loops, and modify variables after each iteration.

Example 4-33 shows how you could write the multiplication table program with a for loop.

Example 4-33. Outputting the 12 times table from a for loop

<?php
for ($count = 1 ; $count <= 12 ; ++$count)
    echo "$count times 12 is " . $count * 12 . "<br />";
?>

See how the entire code has been reduced to a single for statement containing a single conditional statement? Here’s what is going on. Each for statement takes three parameters:

  • An initialization expression

  • A condition expression

  • A modification expression

These are separated by semicolons, like this: for (expr1 ; expr2 ; expr3). At the start of the first iteration of the loop, the initialization expression is executed. In the case of the times table code, $count is initialized to the value 1. Then, each time around the loop, the condition expression (in this case, $count <= 12) is tested, and the loop is entered only if the condition is TRUE. Finally, at the end of each iteration, the modification expression is executed. In the case of the times table code, the variable $count is incremented.

All this structure neatly removes any requirement to place the controls for a loop within its body, freeing it up just for the statements you want the loop to perform.

Remember to use curly braces with a for loop if it will contain more than one statement, as in Example 4-34.

Example 4-34. The for loop from Example 4-33 with added curly braces

<?php
for ($count = 1 ; $count <= 12 ; ++$count)
{
    echo "$count times 12 is " . $count * 12;
    echo "<br />";
}
?>

Let’s compare when to use for and while loops. The for loop is explicitly designed around a single value that changes on a regular basis. Usually you have a value that increments, as when you are passed a list of user choices and want to process each choice in turn. But you can transform the variable any way you like. A more complex form of the for statement even lets you perform multiple operations in each of the three parameters:

for ($i = 1, $j = 1 ; $i + $j < 10 ; $i++ , $j++)
{
    // ...
}

That’s complicated and not recommended for first-time users. The key is to distinguish commas from semicolons. The three parameters must be separated by semicolons. Within each parameter, multiple statements can be separated by commas. Thus, in the previous example, the first and third parameters each contain two statements:

$i = 1, $j = 1  // Initialize $i and $j
$i + $j < 10    // Terminating condition
$i++ , $j++     // Modify $i and $j at the end of each iteration

The main thing to take from this example is that you must separate the three parameter sections with semicolons, not commas (which should be used only to separate statements within a parameter section.)

So, when is a while statement more appropriate than a for statement? When your condition doesn’t depend on a simple, regular change to a variable. For instance, if you want to check for some special input or error and end the loop when it occurs, use a while statement.

Breaking Out of a Loop

You can break out from a for loop using the same break command you use to break out of a switch statement. This step can be necessary when, for example, one of your statements returns an error and the loop cannot continue executing safely.

One case in which this might occur is when writing a file returns an error, possibly because the disk is full (see Example 4-35).

Example 4-35. Writing a file using a for loop with error trapping

<?php
$fp = fopen("text.txt", 'wb');

for ($j = 0 ; $j < 100 ; ++$j)
{
    $written = fwrite($fp, "data");
    if ($written == FALSE) break;
}

fclose($fp);
?>

This is the most complicated piece of code that you have seen so far, but you’re ready for it. We’ll look into the file handling commands in a later chapter; for now all you need to know is that the first line opens the file text.txt for writing in binary mode and then returns a pointer to the file in the variable $fp, which is used later to refer to the open file.

The loop then iterates 100 times (from 0 to 99), writing the string data to the file. After each write, the variable $written is assigned a value by the fwrite function representing the number of characters correctly written. But if there is an error, the fwrite function assigns the value FALSE.

The behavior of fwrite makes it easy for the code to check the variable $written to see whether it is set to FALSE and, if so, to break out of the loop to the following statement closing the file.

If you are looking to improve the code, the line:

if ($written == FALSE) break;

can be simplified using the NOT operator, like this:

if (!$written) break;

In fact, the pair of inner loop statements can be shortened to the following single statement:

if (!fwrite($fp, "data")) break;

The break command is even more powerful than you might think. If you have code nested more than one layer deep that you need to break out of, you can follow the break command with a number to indicate how many levels to break out of, like this:

break 2;

The continue Statement

The continue statement is a little like a break statement, except that it instructs PHP to stop processing the current loop and to move right to its next iteration. So, instead of breaking out of the whole loop, only the current iteration is exited.

This approach can be useful in cases where you know there is no point continuing execution within the current loop and you want to save processor cycles, or prevent an error from occurring, by moving right along to the next iteration of the loop. In Example 4-36, a continue statement is used to prevent a division-by-zero error from being issued when the variable $j has a value of 0.

Example 4-36. Trapping division-by-zero errors using continue

<?php
$j = 10;

while ($j > −10)
{
$j--;
if ($j == 0) continue;
echo (10 / $j) . "<br />";
}
?>

For all values of $j between 10 and −10, with the exception of 0, the result of calculating 10 divided by $j is displayed. But for the particular case of $j being 0, the continue statement is issued and execution skips immediately to the next iteration of the loop.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required