Loops In PHP – Free PHP Tutorials

Loops are used in order to execute some block of data repeatedly or call it iteratively. Just like in other programming languages we have the same looping concepts which are as follows:

  1. while loop
  2. do-while loop
  3. for loop
  4. for-each loop

While Loop Statement in PHP

In this concept of looping the expression inside the loop keeps on executing till the time the expression given as a condition is true. This is an entry controlled loop because if the condition is not true the loop will not execute even once.

Follow the syntax given below:

while(condition)

{

//body of the code

}

Look at the following example:

   <?php
         $x = 0; //variable initialization
         
         
         while( $x < 10) // while loop with conditional check
{
           
            $i++; //variable value incrementation
         }
         
         echo ("Loop stopped at i = $i" );
      ?> 

The output of the code will be:

Loop stopped at i = 10

Do-While Loop Statement in PHP

In this method of looping the code inside the loop is executed at least once whether or not the condition is true or not. Hence this loop is also known as an exit-controlled loop. After the first part is executed the loop then checks the condition and then proceeds further.

Follow the given syntax for implementation:

do

{

//body of the code

}

while(condition)

Look at the following example for clarity:

<?php
         $i = 0;
       
         do { //start of loop
            $i++; increment
         }
         
         while( $i < 10 ); //conditional check at end
         echo ("Loop stopped at i = $i" );
      ?>

The output of the above code is:

Loop stopped at i = 10

For Loop Statement in PHP

This most commonly used looping concept in programming languages. This loop is used when you know how many times you want to execute a part of the code. The check and limit condition is already given in this case.

Follow the given syntax for implementation:

for(initialzation; check condition; increment/decrement)

{

//loop body

}

Look at the following code for more clarity:

<?php
        $num=0; 
         for( $i = 0; $i<5; $i++ )//loop decleration with variable initialization, 
//check condition and increment
 {
           $num=$num+1; //loop operation
         }
         
         echo ("At the end of the loop value of num is $num" );
      ?>

The output of the code is:

At the end of the loop value of num is 5

For-Each Loop in PHP

This looping concept is ideal for iterating through an associative array. Nevertheless, it can be used for the normal arrays too. Here is the syntax for the for-each loop:

for(array as $value)

{

//loop body

}

Look at the following example:

<?php
         $ex = array( 1, 2, 3, 4, 5);
         
         foreach( $ex as $value )//array value taken in another variable {
            echo "Value is $value ";
         }
      ?>

When we talk about looping concepts and their implementation other important concepts also come into play like the following:

Break Statement in PHP

This statement is responsible for coming out of the loop when a certain condition gets satisfied. This statement terminates the loop abruptly After the loop gets terminated, the next statement immediately to the loop gets executed.

Look at the following example:

<?php
        $num=0; 
         for( $i = 0; $i<5; $i++ ) {
           $num=$num+1;
if($num==2){break;} //get out loop at this condition
         }
         
         echo ("At the end of the loop value of num is $num" );
      ?>

Continue Statement in PHP

This statement skips or comes out of the loop after a certain condition gets satisfied for that particular iteration. Then the loop resumes its execution. The continue statement does not terminate the loop in any way. Once the continue statement gets executed it goes for the next pass.

Look at the following example for clarity:

<?php
        $num=0; 
         for( $i = 0; $i<5; $i++ ) {
           $num=$num+1;
           if($num==2)
{echo "this is the condition";
continue; //skip to next step of loop execution at this condition
}

         }
         
         echo ("At the end of the loop value of num is $num" );
      ?>

Leave your queries and doubts in the comment section given.

Leave a Comment

Your email address will not be published. Required fields are marked *