Friday, 2010-07-30

Who's Online

We have 7 guests online


Breadcrumbs

Home For Loop For Loop


For Loop

                                PHP Looping - For Loop

 

Loops execute a block of code a specified number of times, or while a specified condition is true.

 

  • The for Loop


The for loop is used when you know in advance how many times the script should run.

The for loop is the most complex loop in PHP but maybe it is the most used as well. In case of for loops you makes the loop variable initialisation, conditional check, loop variable update in a single line

Syntax:

                                                     for (expresion1; condition; expresion2)
                                                        {
                                                           code to be executed;
                                                        }

 

This syntax requires a bit more explanation.
The expression1 contains the initialisation part of the loop. Here you can set the loop variable like $i=1.
The condition is similar as before. You can write your condition here and it will be checked before each iteration. It means that it can happen that the code block will not executed at all.
The expression2 code is relevant to update the loop variable.

Note:  Each of the parameters above can be empty, or have multiple expressions (separated by commas).

 

Example:

The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:

Code:

                                                                             <html>
                                                                             <body>

                                                                             <?php
                                                                                       for ($i=1; $i<=5; $i++)
                                                                                           {
                                                                                             echo "The number is " . $i . "<br />";
                                                                                           }
                                                                             ?>

                                                                             </body>
                                                                             </html>

 

An output will be:

                                                     The number is 1
                                                     The number is 2
                                                     The number is 3
                                                     The number is 4
                                                     The number is 5

 

  • The foreach Loop

 

The last loop structure in PHP is the foreach. This is a special loop as you can use it only for arrays. The goal of the foreach loop to iterate over each element of an array. If you try to use it with a normal variable you will get an error.

The foreach loop is used to loop through arrays.

Syntax:
                                                     foreach ($array as $value)
                                                              {
                                                                code to be executed;
                                                              }


For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.

 

Example:

The following example demonstrates a loop that will print the values of the given array:

Code:
                                                                                <html>
                                                                                <body>

                                                                                <?php
                                                                                          $x=array("one","two","three");
                                                                                          foreach ($x as $value)
                                                                                                    {
                                                                                                      echo $value . "<br />";
                                                                                                    }
                                                                                ?>

                                                                                </body>
                                                                                </html> 


An output will be:

                                                    one
                                                    two
                                                    three

 

 

    PHP FUNCTION