PHP For Loop

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 times’ table for 12 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 a 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.

 

<< Prev Next >>

 

Loading