Loops basics in PHP

Php loop is used to execute until provided expression is true.

In PHP, four type of loop availiable.

1. WHILE Loop

2. DO – WHILE Loop

3. FOR Loop

4. FOREACH Loop

While statement use for iteration until provided expression is true.

While loop contains expression, check for expression is true or false, if expression is true then Block of Code executed inside while loop until expression will not false.

WHILE LOOP STATEMENTS :

SYNTAX:

while (booleanExpression) {

// Your Code Here….

}

Example1 :

Below example showing Very basic example code for while loop.

<?php
          $a = 1;
		  
         // each iteration while check ($a < 3)
        // if find out ($a < 3) is true execute inner block of code
		
        while ($a < 3) {
		
            print "a = " . $a;
            $a++;  // In each iteration increase a variable value to 1
           
        }
 ?>
 
------------ ****** ------------
OUTPUT:
        a = 1
        a = 2
	

Example 2:

Below example is an infinite loop, don’t run it.

<?php
         while (true) {
		 
            print " infinite loop "
			
          }
?>

Example 3:

We can use break statement to break while loop iteration, After break statement execution will out of while loop execution.

<?php         
        $a = 1;
        while ($a < 10){
		
           print "a = " . $a;
		   
           if($a==6)
             break;
			 
            $a++;  // In each iteration increase a variable value to 1
        }
?>

------------ ****** ------------
OUTPUT:
        a = 1
        a = 2
        a = 3
        a = 4
        a = 5
        a = 6

Example 4 :

Below example is calculating sum of 10 numbers

<?php         
        $a   = 1;
        $NumberSum = 0;
		
		 while ($a <= 10) {
		 
             // Add current a variable value to NumberSum  
             $NumberSum = $NumberSum + $a;
			 
             $a++;  // In each iteration increase a variable value to 1
          }
         print "Sum of numbers  = " . $NumberSum; 
 ?>
 
 ------------ ****** ------------
  OUTPUT:
	     Sum of numbers = 55
       

 

DO WHILE LOOP: :

Do While statement also use for iteration until provided expression (condition) is true.

Do While loop firstly runs code once then check for expression is true or false, if expression is true then Code executed again until expression will not false.

SYNTAX:

do{

// You Code Here….

}while (booleanExpression);

Example 5 :

Below code show do-while execute at least once.

<?php
        $x = 1;
		
         do {
               print "x = " . $x;
               $x++;
           }     
           while ($x > 2);  // expression false
?>

 ------------ ****** ------------
  OUTPUT:
	     x = 1

Example 6 :

Below example is calculating sum of 10 numbers using do-while loop

<?php  
      
       $a   = 1;
       $NumberSum = 0;
	   
       do {
	   
           // Add current a variable value to NumberSum  
           $NumberSum = $NumberSum + $a;
           $a++;  // In each iteration increase a variable value to 1
		   
         }while ($a <= 10);  // Condition
		 
        print "Sum of numbers  = " . $NumberSum; 
?>

 ------------ ****** ------------
  OUTPUT:
	     Sum of numbers = 55

FOR LOOP :

For loop statement use for iteration until provided expression (condition) is true.

For loop check for expression is true or false, if expression is true then Code executed inside for loop until expression will not false.

SYNTAX:

for ( initialize variable ; booleanExpression ; update initialize variable) {

// You Code Here….

*

}

 

initialize variable : initilize one or many variables. Ex. int i=0

booleanExpression : $i < 5

update initialize variable : update one or many variables. Ex. i++

See Below for loop examples

Example 6 :

<?php 
        
        for (int $a = 0; $a < 5; $a++) {
		
              print "a = " . $a;
        }
?>

------------ ****** ------------
OUTPUT:
        a = 0
        a = 1
        a = 2
        a = 3
        a = 4

Example 7 :

Taking example for use of || operator.

<?php     
   
        $a = 0;
        for ( ; $a < 5; $a++) {
		
             print "a = " . $a;
			 
        }
 ?>
 
------------ ****** ------------
 OUTPUT:
        a = 0
        a = 1
        a = 2
        a = 3
        a = 4

Example 8:

<?php         
        $a = 0;
        for ( ; $a < 5 ; ) {
		
             print "a = " . $a);
             $a++;
			  
         }
 ?>
  
------------ ****** ------------
 OUTPUT:
        a = 0
        a = 1
        a = 2
        a = 3
        a = 4
 
 

Example 9 :

<?php  
       
        $a = 0;
        for ( ;  ; ) {
		
            print "a = " .$a);
			
            if($a > 4)
                break;  // Loop will when check $a value is greater than 4
				
             $a++;
			 
         }
?>

------------ ****** ------------
 OUTPUT:
        a = 0
        a = 1
        a = 2
        a = 3
        a = 4

Example 10:

Below example is calculating sum of 10 numbers

<?php
         
      $NumberSum = 0;
        
	  for ( $a=1 ; $a <= 10 ; $a++) {
		
          // Add current a variable value to NumberSum  
          $NumberSum = $NumberSum + $a;
		  
        }
		
        print "Sum of numbers  = " + $NumberSum); 
         
?>

------------ ****** ------------
 OUTPUT:
       Sum of numbers = 55

Example 11 :

Below example is infinite loop.

<?php 
        
        for (  ; ; ) {
		
           print "Infinite Loop"
		   
         }
 
?>

Example 12 :

Below example is showing how to take multiple initialization and updation in for loop

<?php
         
        for ($x = 0, $y = 4; $x < 5; $x++, $y--){
		
              print "i = " + $x + " , j= " + $y);
			  
          }
?>

------------ ****** ------------
 OUTPUT:
       	i = 0 , j= 4
	    i = 1 , j= 3
	    i = 2 , j= 2
		i = 3 , j= 1
		i = 4 , j= 0
		

4. FOREACH loop statement :

FOREACH Loop is used to iterate array values or for objects.

In each iteration we will get array key and value pair.

SYNTAX:

foreach (array as key => value) {

//code to be executed;

}

Example :

<?php
         $array = array( 11, 12, 13, 14, 15);
         
         foreach( $array as $key => $value ) {
		 
            echo "Value at Index($key) is $value <br />"
         }
?>

Example:

<?php

     $array = array("foreach", "loop", "array", "example");
         
     foreach( $array as $key => $value ) {
		 
            echo "Value at Index($key) is $value <br />"
     }
?>    

Break statement :

In Php, break keyword is used to terminate the loop.

We will use break statement inside any loop code block, whenever loop break statement will find out, loop will terminate the code execution and jump out side the loop block.

Example :

<?php
         
		 $array = array("array", "foreach", "loop", "break", "example");
         
		 print "LOOP Start<br />";
		 
         foreach( $array as $key => $value ) {
		 
            if($value == "array"){
			
			    print "Break called  <br />";
			    break;
				
			  }
			  
			 echo "Value at Index($key) is $value <br />";
         }
		 
		 print "Outside LOOP. <br />";
 ?>
 
 

Continue statement :

In loop, whenever continue statement will call, loop will break for particular that value excution and again execute for next loop value.

<?php

         $array = array("foreach", "loop", "continue", "example");
         
		 print "LOOP Start<br />";
		 
         foreach( $array as $key => $value ) {
		 
            if($value == "loop"){
			
			    print "Continue called  <br />";
			    continue;
			  }
			  
			 echo "Value at Index($key) is $value <br />";
         }
		 
		 print "Outside LOOP. <br />";
?>