Loop inside a loop is called nested loop.In php allows using one loop inside another loop. The following section shows few examples to illustrate the concept. nested loop syntax and suitable example are as under…

here, To manage this loop execution we will also discuss about break and continue keywords which used to control the loop’s execution.

 

Nested while loop in php :

Syntax :

while(condition)
{
    while(condition)
     {
        statement(s);
        increment/decrements;
     }
    statement(s);
    increment/decrements;
}

 

Example:

<?php
int $i = 1 , $j = 1;
while( $i < 3 )
{
    while( $j < 3 )
    {
        echo ‘i am programmer  ‘;
        $j++;
    }
    echo ‘<br />’;
    $i++;
}
?>

Output:
i am programmer i am programmer
i am programmer i am programmer

 


Nested do while loop in php :

Syntax:

do
{
   statement(s);
   increment/decrements;
   do
   {
      statement(s);
      increment/decrements;
   }while( condition );
}while( condition );

 

Example:

<?php
int $i=1 , $j=2;
do
{
   do
   {
      echo ‘i am programmer  ‘;
      $j++;
   }while( $j < 3 )
   echo ‘<br />’;
   $i++;
}while( $i < 3 )
?>

Output:
i am programmer i am programmer
i am programmer i am programmer

 


Nested for loop in php :

Syntax:

for ( initialization; condition; increment/decrements )
{
   for ( initialization; condition; increment/decrements )
   {
      statement(s);
   }
   statement(s);
}

 

Example:

<?php
int $i , $j;
for( $i=1; $i<3; $i++ )
{
   for( $j=1; $j<3; $j++
   {
      echo ‘i am programmer  ‘;
   }
   echo ‘<br />’;
}
?>

Output:
i am programmer i am programmer
i am programmer i am programmer

 


Nested Foreach loop in php :

Syntax:

foreach(array as value)
{
   foreach(array as value)
   {
      Statement;
   }
   Statement;
}

 

Example:

<?php
$color= array( “Red”, “Green”, “Yellow”, );
$fruit = array( “Banana”, “Apple”, “Plum”, );
foreach ($color as $c)
{
   foreach ($fruit as $f)
   {
      echo “this is A $c $f .<br/>\n”;
   }
}
?>

Output:
this is A Red Banana.
this is A Red Apple.
this is A Red Plum.
this is A Green Banana.
this is A Green Apple.
this is A Green Plum.
this is A Yellow Banana.
this is A Yellow Apple.
this is A Yellow Plum.

 

Now, We will discuss about continue, break, return and exit keywords used to control the loop’s execution.

 

The break statement :

The PHP break keyword is used to terminate the execution of a loop prematurely.

The break statement is situated inside the statement block. It gives you full control and whenever you want to exit from the loop you can come out. After coming out of a loop immediate statement to the loop will be executed.

 

Example:

<?php
$i = 0;
while( $i < 5)
{
   $i++;
   if( $i == 2 )break;
}
echo (“Loop stopped at i = $i” );
?>

Output:
Loop stopped at i = 2

 


The continue statement:

The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.

Just like the break statement, the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. For the pass encountering continue statement, rest of the loop code is skipped and next pass starts.

 

Example:

<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{
   if( $value == 3 )continue;
   echo “Value is $value <br />”;
}
?>

Output:
Value is 1
Value is 2
Value is 4
Value is 5

 


The return statement:

If called from within a function, the return statement immediately ends execution of the current function and returns its argument as the value of the function call.

For those of you who think that using return in a script is the same as using exit note that: using return just exits the execution of the current script, exit the whole execution.

a.php file:

<?php
include(“b.php”);
echo “a”;
?>

 

b.php file:

<?php
echo “b”;
return; //or exit;
?>

 

Output:

while executing the a.php file with the return keyword in file b.php, it will show output: “ba”.
whereas executing the a.php file with the return keyword in file b.php, it will show output :  “b”.