The for Statement


The for Statement

 for (expr1; expr2; expr3) statement 

The for loop is the most involved loop in PHP. In this statement, the first expression, expr1, is evaluated once at the beginning of the loop. The continuation expression, expr2, is evaluated at the beginning of each iteration. If it evaluates to TRUE, the loop iterates again, and the nested statement(s) are executed. If it evaluates to FALSE, the loop ends. At the end of each iteration, expr3 is executed, which usually increments a loop counter variable that will be tested in expr2.

Here's an example, where the for loop will display text twenty times:

 <?php     for ($loop_counter = 0; $loop_counter < 20; $loop_counter++){         echo "This will print twenty times.<BR>";     } ?> 

In fact, you can reduce this loop to the following script, where the for loop has no body at all:

 <?php     for ($loop_counter = 0; $loop_counter < 20; echo "This will print twenty         times.<BR>", $loop_counter++); ?> 



    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