PHP Loop structure

Loops are normally used for iterations, which is useful when some set of statements needs to be processed number of times. In PHP there are different types of loops provided as listed below.

  1. while loop
  2. do while loop
  3. for loop
  4. foreach loop

While loop

In this kind of loop the set of statements will be processed each time a given condition in while loop is satisfied when the condition goes false loop terminates. The loop iteration is done manually inside the body of loop.

Syntax

while(condition)
{
       Statements to be processed
       loop iteration (increment/decrement)
}

For Ex.

<?php
       $sum=0;
       $i=1;
       while($i<=10)
       {
             $sum=$sum+$i;
             $i++;
       }
       echo "sum from 1 to 10 is ".$sum;
?>

As shown in above example the condition for while loop is $i<=10 while this condition is satisfied given set of statements in while loop will be executed , when the condition goes false the loop will be terminated , here $i++ is a loop iteration variable which increments the value of $i to 1 every time.  The program will print 55 as output which is sum of numbers from 1 to 10.

Do while Loop

Do while loop is almost as same as while loop with some differences , do while loop is also known as exit control loop, because in do while loop the loop body is executed first and then after the condition is checked . in other words the condition is checked on exit.

Syntax

do
{
      Statements to be processed
      loop iteration (increment/decrement)
}
while(condition);

For Ex.

<?php
       $sum=0;
       $i=1;
       do
       {
             $sum=$sum+$i;
             $i++;
        }while($i<=10);
       echo "SUM from 1 to 10 is ".$sum;
?>

As you can see in the example the condition is given after the loop body so it will be checked once a loop body is executed so when you use do while loop it will be executed at least once where as in while loop the loop body is executed on basis of condition.

Difference between While & Do while

  • While loop is entry control loop where as do while loop is exit control loop.
  • do while loop is executed at least once regardless of condition whereas while loop is executed on basis of condition
  • do while loop is used when you want to execute the loop body at least once

For Loop

For loop is alternate of while loop, in for loop initialization, condition & loop iteration is given at first.

Initialization: - initial value to be used for loop variable.

Condition: - The condition to be satisfied for loop to be executed.

Iteration:-  increment or decrement of loop variable

Syntax

for (initialization; condition; iteration)
{
       Statements to be processed
}

For Ex.

<?php
       $sum=0;
       for($i=0;$i<=10;$i++)
       {
             $sum=$sum+$i;
       }
       echo "sum from 1 to 10 is ".$sum;
?>

The example is as same as the one used in while loop the only difference with for loop is that all three things initialization, condition & iteration is specified when you use for loop, where as in while loop the loop variable was initialized before while loop and the iteration was used inside the while loop body.

Foreach Loop

This is a special type of loop which is used for iterating through array. While using arrays in which number of elements in array is not fixed in prior in such case foreach loop is used . foreach loop is specially used for iterating through associative arrays( do not get confused we will learn what is associative array later). For now lets just learn how a basic foreach loop will be  used we will learn its use in detail when we learn array.

Syntax

foreach($arrayname as $varname)
{
       Statements to be processed
}

For Ex.

<?php
       $a=array(1,2,3,4,5);
       foreach($a as $var)
       {
              echo $var."<br>";
       }
?>

As you can see in the above example to iterate through array $a we have used foreach loop , each time a foreach loop executed one element from array $a is copied into variable $var the loop is repeated till we have any elements left in array. In sort it will take one element of array and copy it into variable specified.

So to conclude this tutorial we have learnt different loop structure with their basic use.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments