1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Looping in PHP

Discussion in 'PHP' started by Sanskruti, Apr 14, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    Repetitive tasks are always a burden to us. Deleting spam email, sealing 50 envelopes, and going to work are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such repetitive tasks with a little bit of extra thinking. Most often these repetitive tasks are conquered in the loop.The idea of a loop is to do something over and over again until the task has been completed.

    In PHP we have the following looping statements:
    • while - loops through a block of code if and as long as a specified condition is true
    • do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true
    • for - loops through a block of code a specified number of times
    • foreach - loops through a block of code for each element in an array

    The while Statement



    The while statement will execute a block of code if and as long as a condition is true.

    Syntax

    Code:
    while (condition)
    {
    	code to be executed;
    }
    Example

    The following example demonstrates a loop that will continue to run as long as the variable i is less than, or equal to 5. i will increase by 1 each time the loop runs:
    Code:
    <html>
    <body>
    <?php 
    $i=1;
    while($i<=5)
    {
      echo "The number is " . $i . "<br />";
      $i++;
    }
    ?>
    </body>
    </html>

    The do...while Statement



    The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.

    Syntax

    Code:
    do
    {
    	code to be executed;
    }
    while (condition);
    
    Example

    The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 5:
    Code:
    <html>
    <body>
    <?php 
    $i=0;
    do
    {
      $i++;
      echo "The number is " . $i . "<br />";
    }
    while ($i<5);
    ?>
    </body>
    </html>

    The for Statement



    The for statement is used when you know how many times you want to execute a statement or a list of statements.

    Syntax

    Code:
    for (initialization; condition; increment)
    {
      code to be executed;
    }
    The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop. If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.

    Example

    The following example prints the text "Hello World!" five times:

    Code:
    <html>
    <body>
    <?php
    for ($i=1; $i<=5; $i++)
    {
      echo "Hello World!<br />";
    }
    ?>
    </body>
    </html>
    

    The foreach Statement



    The foreach statement is used to loop through arrays. For every loop, the value of the current array element is assigned to $value so on the next loop.

    Syntax

    Code:
    foreach (array as value)
    {
        code to be executed;
    }
    Example

    The following example demonstrates a loop that will print the values of the given array:
    Code:
    <html>
    <body>
    <?php
    $arr=array("one", "two", "three");
    foreach ($arr as $value)
    {
      echo "Value: " . $value . "<br />";
    }
    ?>
    </body>
    </html>
     
  2. Magena

    Magena Banned

    Joined:
    Jul 12, 2008
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    Hi


    thanks for the article. I m a beginner in PHP and now i m in looping chapter. I think i will facing some prob with this can you sort out this.

    thanks
     
  3. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    In programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping statements. PHP has two major groups of looping statements: for and while. The For statements are best used when you want to perform a loop a specific number of times. The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the Break and Continue statements within looping statements.
    exemples:-

    The While Loop
    Code:
                while (condition)
                  {
                      code needed to loop;
                   }
    
    The Do...While Loop
    Code:
          do
              {
                  code needed to loop;
              }
               while (condition);
    
    The For Loop
    Code:
     
           for (initialization; condition; increment)
                   {
                      code needed to loop;
                    }
    
    The Foreach Loop
    Code:
            foreach (array as value)
                {
                    code needed to loop;
                }
    
    Break and Continue Statements
    PHP:
               <?php
                 
    for ($i 0$i <= 10$i++)
                        {
                            if(
    $i==5)
                                {
                                    break;
                                 }
                echo 
    "The number is " $i "<br/>";
                }
              
    ?>
     
    Last edited by a moderator: Jun 18, 2009
  4. shrikrishnatech

    shrikrishnatech New Member

    Joined:
    Nov 19, 2010
    Messages:
    42
    Likes Received:
    4
    Trophy Points:
    0
    Occupation:
    Wordpress theme, Wordpress Theming, WP Themes, Cor
    Home Page:
    thanks for your complete details
     
  5. cherylfoster

    cherylfoster New Member

    Joined:
    Jan 3, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Sequence of instructions in an infinite loop the loop endlessly computer program, either due to cyclic non-termination conditions, one can never meet, or to cycle to start again. In the old cooperative multitasking operating system, an infinite loop is usually cause the entire system to stop responding. And now, the popular model of preemptive multitasking, infinite loops usually cause the program consumes all available processor time, but usually can be terminated by the user. Busy wait loop is sometimes mistakenly called "infinite loop. " One possible reason for a computer "freeze"is an infinite loop, the other including the deadlock and an access violation.
     
  6. cartergarth

    cartergarth New Member

    Joined:
    Jul 25, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    a good beginners guide to php and how it uses loops. :)
     
  7. ankitasharma

    ankitasharma Banned

    Joined:
    Jul 23, 2011
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    The common tasks that are covered by a for loop are:

    1. Set a counter variable to some initial value.
    2. Check to see if the conditional statement is true.
    3. Execute the code within the loop.
    4. Increment a counter at the end of each iteration through the loop.

    The for loop allows you to define these steps in one easy line of code. It may seem to have a strange form, so pay close attention to the syntax used!

    Let us take the example from the while loop lesson and see how it could be done in a for loop. The basic structure of the for loop is as follows:
    Pseudo PHP Code:

    for ( initialize a counter; conditional statement; increment a counter)
    {
    do this code;
    }

    Notice how all the steps of the loop are taken care of in the for loop statement. Each step is separated by a semicolon: initiliaze counter, conditional statement, and the counter increment. A semicolon is needed because these are separate expressions. However, notice that a semicolon is not needed after the "increment counter" expression.
     

Share This Page