Working with for Loops


Working with for Loops

One of the things that computers excel at is handling repetitive tasks. For example, say you have 10,000 student scores to average; adding them all up manually would take a lot of timebut it's no problem for a computer. Computers handle repetitive tasks such as this with loops, and the first loop we're going to take a look at is the for loop, which when repeated executes a statement (which can be a compound statement):

 for (expression1; expression2; expression3) statement 

In this statement, expression1 lets you set up your loop, often by initializing a loop counter variable (also called a loop index) that will track how many times the loop has executed. This expression is executed before the loop's statement is run each time through the loop. The next expression, expression2, is the test expressionthe loop will keep going while this expression remains true. You usually test the value in your loop counter here. The final expression, expression3, is executed after statement is executed, each time through the loop. You usually increment your loop counter variable in this expression.

Here's a simple example, phpfor.php, Example 2-7, to make this all clear. In this case, we'll use a for loop to display a line of text five times. The loop starts by setting a loop counter variable that we'll name $loop_counter to 0. The loop increments the counter each time after it runs and then tests to make sure that the loop counter doesn't exceed 5.

Example 2-7. Using the for loop, phpfor.php
 <HTML>     <HEAD>         <TITLE>             Using the for loop         </TITLE>     </HEAD>     <BODY>         <H1>             Using the for loop         </H1>         <?php             for ($loop_counter = 0; $loop_counter < 5; $loop_counter++){                 echo "I'm going to do this five times.<BR>";             }         ?>     </BODY> </HTML> 

The results appear in Figure 2-8, where, as you can see, the text is displayed five times.

Figure 2-8. Using the for loop.


The expressions in a for loop can handle multiple indexes if you want, as long as you separate them with the comma operator. Here's an example that uses two loop indexes:

 <?php for ($var1=2, $var2=2; $var1 < 5 && $var2 < 5; $var1++, $var2++){     echo "$var1 x $var2 = ", $var1 * $var2, "<BR>"; } 

?>

And here's what the result looks like in a browser:

 2 x 2 = 4 3 x 3 = 9 4 x 4 = 16 

You don't have to use a loop index at all in a for loop. This example uses some (fictitious) functions to connect to a data source over the Internet, process the received data, and move to the next data item as the loop progresses:

 for (initialize_connection(); process_new_data() != "quit"; move_to_next_item()){     echo "Getting more data...."; } 

You can also nest for loops, one inside another, as we'll see when we work with arrays.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

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