Loops

So far we've looked at decisions that a script can make about what code to execute. Scripts can also decide how many times to execute a block of code. Loop statements are designed to enable you to achieve repetitive tasks. Almost without exception, a loop continues to operate until a condition is achieved, or you explicitly choose to exit the loop.

The while Statement

The while statement looks similar in structure to a basic if statement:

 while ( expression ) {       // do something } 

As long as a while statement's expression evaluates to true, the code block is executed over and over again. Each execution of the code block in a loop is called an iteration. Within the block, you usually change something that affects the while statement's expression; otherwise, your loop continues indefinitely. Listing 5.6 creates a while loop that calculates and prints multiples of 2 up to 24.

Listing 5.6 A while Statement
   1: <html>   2: <head>   3: <title>Listing 5.6</title>   4: </head>   5: <body>   6: <?php   7: $counter = 1;   8: while ( $counter <= 12 ) {   9:     print "$counter times 2 is ".($counter*2)."<br>";  10:     $counter++;  11: }  12: ?>  13: </body>  14: </html> 

In this example, we initialize a variable $counter in line 7. The while statement in line 8 tests the $counter variable. As long as the integer that $counter contains is less than or equal to 12, the loop continues to run. Within the while statement's code block, the value contained by $counter is multiplied by two, and the result is printed to the browser. Then line 10 increments $counter. This last stage is extremely important. If you were to forget to change $counter, the while expression would never resolve to false, and the loop would never end.

Put these lines into a text file called testwhile.php, and place this file in your Web server document root. When you access this script through your Web browser, it produces the following:

 1 times 2 is 2 2 times 2 is 4 3 times 2 is 6 4 times 2 is 8 5 times 2 is 10 6 times 2 is 12 7 times 2 is 14 8 times 2 is 16 9 times 2 is 18 10 times 2 is 20 11 times 2 is 22 12 times 2 is 24 

The do...while Statement

A do...while statement looks a little like a while statement turned on its head. The essential difference between the two is that the code block is executed before the truth test and not after it:

 do   {      // code to be executed } while ( expression ); 

graphics/book.gif

The test expression of a do...while statement should always end with a semicolon.


This statement might be useful if you want the code block to be executed at least once even if the while expression evaluates to false. Listing 5.7 creates a do...while statement. The code block is executed a minimum of one time.

Listing 5.7 The do...while Statement
   1: <html>   2: <head>   3: <title>Listing 5.7</title>   4: </head>   5: <body>   6: <?php   7: $num = 1;   8: do {   9:     print "Execution number: $num<br>\n";  10:     $num++;  11: } while ( $num > 200 && $num < 400 );  12: ?>  13: </body>  14: </html> 

The do...while statement tests whether the variable $num contains a value that is greater than 200 and less than 400. In line 7, we have initialized $num to 1, so this expression returns false. Nonetheless, the code block is executed before the expression is evaluated, so the statement will print a single line to the browser.

Put these lines into a text file called testdowhile.php, and place this file in your Web server document root. When you access this script through your Web browser, it produces the following:

 Execution number: 1 

The for Statement

You cannot achieve anything with a for statement that you cannot do with a while statement. On the other hand, the for statement is often a neater and safer way of achieving the same effect. Earlier, Listing 5.6 initialized a variable outside the while statement. The while statement then tested the variable in its expression. The variable was incremented within the code block. The for statement allows you to achieve this on a single line. This allows for more compact code and makes it less likely that you will forget to increment a counter variable, thereby creating an infinite loop:

 for ( initialization expression; test expression; modification expression ) {     // code to be executed } 

graphics/book.gif

Infinite loops are, as the name suggests, loops that run without bounds. If your loop is running infinitely, your script is running for an infinite amount of time. This is very stressful on your Web server, and renders the Web page in question unusable.


The expressions within the parentheses of the for statement are separated by semicolons. Usually, the first expression initializes a counter variable, the second expression is the test condition for the loop, and the third expression increments the counter. Listing 5.8 shows a for statement that re-creates the example in Listing 5.6, which multiplies 12 numbers by 2.

Listing 5.8 Using the for Statement
   1: <html>   2: <head>   3: <title>Listing 5.8</title>   4: </head>   5: <body>   6: <?php   7: for ( $counter=1; $counter<=12; $counter++ ) {   8:     print "$counter times 2 is ".($counter*2)."<br>";   9: }  10: ?>  11: </body>  12: </html> 

Put these lines into a text file called testfor.php, and place this file in your Web server document root. When you access this script through your Web browser, it produces the following:

 1 times 2 is 2 2 times 2 is 4 3 times 2 is 6 4 times 2 is 8 5 times 2 is 10 6 times 2 is 12 7 times 2 is 14 8 times 2 is 16 9 times 2 is 18 10 times 2 is 20 11 times 2 is 22 12 times 2 is 24 

The results of Listings 5.6 and 5.8 are exactly the same. The for statement, though, makes the code more compact. Because $counter is initialized and incremented at the top of the statement, the logic of the loop is clear at a glance. In line 7, within the for statement's parentheses, the first expression initializes the $counter variable and sets it to 1. The test expression verifies that $counter contains a value that is less than or equal to 12. The final expression increments the $counter variable.

When program flow reaches the for loop, the $counter variable is initialized, and the test expression is evaluated. If the expression evaluates to true, the code block is executed. The $counter variable is then incremented and the test expression is evaluated again. This process continues until the test expression evaluates to false.

Breaking Out of Loops with the break Statement

Both while and for statements incorporate a built-in test expression with which you can end a loop. The break statement, though, enables you to break out of a loop based on the results of additional tests. This can provide a safeguard against error. Listing 5.9 creates a simple for statement that divides a large number by a variable that is incremented, printing the result to the screen.

Listing 5.9 A for Loop That Divides 4000 by 10 Incremental Numbers
   1: <html>   2: <head>   3: <title>Listing 5.9</title>   4: </head>   5: <body>   6: <?php   7: for ( $counter=1; $counter <= 10; $counter++ ) {   8:       $temp = 4000/$counter;   9:       print "4000 divided by $counter is... $temp<br>";  10: }  11: ?>  12: </body>  13: </html> 

In line 7, this example initializes the variable $counter to 1. The for statement's test expression verifies that $counter is less than or equal to 10. Within the code block, 4000 is divided by $counter, printing the result to the browser.

Put these lines into a text file called testfor2.php, and place this file in your Web server document root. When you access this script through your Web browser, it produces the following:

 4000 divided by 1 is... 4000 4000 divided by 2 is... 2000 4000 divided by 3 is... 1333.33333333 4000 divided by 4 is... 1000 4000 divided by 5 is... 800 4000 divided by 6 is... 666.666666667 4000 divided by 7 is... 571.428571429 4000 divided by 8 is... 500 4000 divided by 9 is... 444.444444444 4000 divided by 10 is... 400 

This seems straightforward enough. But what if the value you place in $counter comes from user input? The value could be a negative number, or even a string. Let's take the first instance. Changing the initial value of $counter from 1 to -4 causes 4000 to be divided by 0 when the code block is executed for the fifth time. It is generally not a good idea for your code to divide by zero, and Listing 5.10 guards against this by breaking out of the loop if the $counter variable equals zero.

Listing 5.10 Using the break Statement
   1: <html>   2: <head>   3: <title>Listing 5.10</title>   4: </head>   5: <body>   6: <?php   7: $counter = -4;   8: for ( ; $counter <= 10; $counter++ ) {   9:     if ( $counter == 0 )  10:          break;  11:      $temp = 4000/$counter;  12:      print "4000 divided by $counter is... $temp<br>";  13: }  14: ?>  15: </body>  16: </html> 

graphics/book.gif

Dividing a number by zero does not cause a fatal error in PHP. Instead, PHP generates a warning and execution continues.


We use an if statement, shown in line 9, to test the value of $counter. If it is equal to zero, the break statement immediately halts execution of the code block, and program flow continues after the for statement.

Put these lines into a text file called testfor3.php, and place this file in your Web server document root. When you access this script through your Web browser, it produces the following:

 4000 divided by -4 is... -1000 4000 divided by -3 is... -1333.33333333 4000 divided by -2 is... -2000 4000 divided by -1 is... -4000 

Notice that we initialize the $counter variable in line 7, outside the for statement's parentheses, to simulate a situation in which the value of $counter is set according to form input or a database lookup.

graphics/bulb.gif

You can omit any of the expressions from a for statement, but you must remember to retain the semicolons.


Skipping an Iteration with the continue Statement

The continue statement ends execution of the current iteration but doesn't cause the loop as a whole to end. Instead, the next iteration begins immediately. Using the break statement as we did in Listing 5.10 is a little drastic. With the continue statement in Listing 5.11, you can avoid a divide by zero error without ending the loop completely.

Listing 5.11 Using the continue Statement
   1: <html>   2: <head>   3: <title>Listing 5.11</title>   4: </head>   5: <body>   6: <?php   7: $counter = -4;   8: for ( ;  $counter <= 10; $counter++ ) {   9:     if ( $counter == 0 ) {  10:         continue;  11:     }  12:     $temp = 4000/$counter;  13:     print "4000 divided by $counter is... $temp<br>";  14: }  15: ?>  16: </body>  17: </html> 

In line 10, we have swapped the break statement for a continue statement. If the $counter variable is equivalent to zero, the iteration is skipped, and the next one starts immediately.

Put these lines into a text file called testcontinue.php, and place this file in your Web server document root. When you access this script through your Web browser, it produces the following:

 4000 divided by -4 is... -1000 4000 divided by -3 is... -1333.33333333 4000 divided by -2 is... -2000 4000 divided by -1 is... -4000 4000 divided by 1 is... 4000 4000 divided by 2 is... 2000 4000 divided by 3 is... 1333.33333333 4000 divided by 4 is... 1000 4000 divided by 5 is... 800 4000 divided by 6 is... 666.666666667 4000 divided by 7 is... 571.428571429 4000 divided by 8 is... 500 4000 divided by 9 is... 444.444444444 

graphics/clock.gif

The break and continue statements can make code more difficult to read. Because they often add layers of complexity to the logic of the loop statements that contain them, you should use them with care.


Nesting Loops

Loop statements can contain other loop statements. The combination of such statements is particularly useful when working with dynamically created HTML tables. Listing 5.12 uses two for statements to print a multiplication table to the browser.

Listing 5.12 Nesting Two for Loops
   1: <html>   2: <head>   3: <title>Listing 5.12</title>   4: </head>   5: <body>   6: <?php   7: print "<table border=\"1\">\n";   8: for ( $y=1; $y<=12; $y++ ) {   9:     print "<tr>\n";  10:     for ( $x=1; $x<=12; $x++ ) {  11:         print "\t<td>";  12:         print ($x*$y);  13:         print "</td>\n";  14:     }  15:     print "</tr>\n";  16: }  17: print "</table>";  18: ?>  19: </body>  20: </html> 

Before we examine the for loops, let's take a closer look at line 7 in Listing 5.12:

 print "<table border=\"1\">\n"; 

Notice that we have used the backslash character (\) before each of the quotation marks within the string. This is necessary in order to tell the PHP engine that we wish to use the quotation mark character, rather than interpret it as the beginning or end of a string. If we did not do this, the statement would not make sense to the engine, which would read it as a string followed by a number followed by another string. This would generate an error. In this listing, we use \n to represent a newline character, and \t to represent a tab character.

The outer for statement (line 8) initializes a variable called $y, setting its starting value to 1. It defines an expression that verifies that $y is less than or equal to 12 and defines the increment for $y. For each iteration, the code block prints a tr (table row) HTML element (line 9) and defines another for statement (line 10). This inner loop initializes a variable called $x and defines expressions along the same lines as for the outer loop. For each iteration, the inner loop prints a td (table cell) element to the browser (line 11), as well as the result of $x multiplied by $y (line 12). In line 13, we close the table cell. After the inner loop has finished, we fall back through to the outer loop, where we close the table row on line 15, ready for the process to begin again. When the outer loop has finished, the result is a neatly formatted multiplication table. We wrap things up by closing the table on line 17.

Put these lines into a text file called testnestfor.php, and place this file in your Web server document root. When you access this script through your Web browser, it should look like Figure 5.1.

Figure 5.1. Output of Listing 5.12.

graphics/05fig01.gif



Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net