Looping Control Statement in PHP


Sometimes it is required to perform same task again and again repeatadely in the script. In such situations Looping Control Statements are used in PHP.
Using looping control statement same task can be performed repeatadely for specific amount of time or until desired condition is not satisfied.
PHP supports following Looping Control Statements:
(1) While Loop
(2) Do...While Loop
(3) For Loop
(4) Foreach Loop

While Loop


Syntax   
     while (condition)
     {
           body of loop;
     }

While loop first check the condition. If condition is true then the statements written inside body of loop are executed. After executing statements inside body of loop again the condition is checked.
This process is repeated until condition specified within while loop becomes false. As the condition becomes false the statements inside body of loop are not executed and the statements followed by the while loop is executed.
It is also known as entry controlled loop because the condition is checked first and then body of the loop is executed based on the condition. So if condition is false at the first trial the body of loop will never executes.

Example
<?php

    $i=1;
   while($i<=10)
   {
           echo $i." ";
       $i++;
   }
?>
Output
1 2 3 4 5 6 7 8 9 10 

     

Do While Loop


Syntax
     do
     {
           body of loop;
     } while (condition);

In do … while loop first statements written inside body of the loop are executed. After executing statements inside body of the loop the condition is checked.  If condition is true then the statements written inside body of loop are executed again.
This process is repeated until condition specified within do … while loop becomes false. As the condition becomes false the statements inside body of loop is not executed and the statements followed by the do … while loop is executed.
It is also known as exit controlled loop because the condition is checked at last and then body of the loop is executed based on the condition. So even if condition is false at the first trial the body of loop executes at least once.  

Example
<?php

    $i=1;
   do
   {
      echo $i." ";
       $i++;
   }while($i<=10);
Output
1 2 3 4 5 6 7 8 9 10 

For Loop


Syntax
    for(initialization;condition;iteration)
     {
           body of loop;
     }
Here,
Initialization: It indicates the starting point for the loop. Starting point specifies from which value the loop will start the iteration.
Condition:It indicates control condition for the loop. Control condition determines when loop will stop.
Iteration:It indicates how the value of the counter variable that holds the starting value will vary. You can either increment or decrement the value of the counter variable based on your requirement.

for loop is used to repeatedly perform some task for specific number of times. It is also known as iterative statement. It is useful when you know in advance how many times you want to repeat the task.
In for loop first the counter variable is initialized with the starting value in the initialization part. After initialization the condition is checked. If the condition is true then the statements written inside body of loop will executes. After executing statements inside body of loop the control goes to the Iteration part where the value of the counter variable is incremented or decremented. After changing the value of the counter variable it again check the condition.
This process is repeated until condition specified within for loop becomes false. As the condition becomes false the statements inside body of loop will not executes and control transfer immediately to the statement followed by for loop. 

Example
<?php

   for ($i=1;$i<=10;$i++)
   {
        echo $i. " ";
   }
?>
Output
1 2 3 4 5 6 7 8 9 10 

foreach loop


Syntax
foreach ($ArrayName as $Value)
{
   Body of loop

foreach looping structure is used to iterate throgh each elements of an array starting from first element.
In foreach loop how many times the loop will iterate depands on the number of elements in an array.
foreach looping structure is used specially with Associative array.

Example
<?php
$MyArray = array (Yesha, Vidhi, Jiwansh);
foreach ($MyArray as $Value)
{
   echo $value. " ";

?>
Output
Yesha  Vidhi  Jiwansh
 

Download Projects


Download Programs