A loop inside a loop in PHP

PHP Nested Loop

Nested loops in PHP

In This article we will talk about nested loops in PHP. A loop inside another loop is called a nested loop.
Below we give some examples for illustrating the concept of nested loops and the convenience of their use.

Nested WHILE loop in PHP:

This cycle repeats WHILE our condition is true and checks the value with each pass at the beginning of the cycle, and if the condition is not true, the cycle will not be executed even once.

Syntax of the while loop inside the while loop:

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

An example of a nested while loop:

<?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

The Do While loop is different from the While loop by one feature, it checks the truth of the expression at the end of the iteration.  This is guarantees the execution of a loop at least once and this different it from the usual While loop in PHP.

We mentioned above the usual While loop may fail if the first value is not true.

The Do While loop syntax inside a Do While loop:

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

PHP nesting the For loop inside a For loop

The For loop in php is not more complicated than the previous ones, it is different in that there is a counter in it.

Simply put, we declare a cycle and in parentheses we indicate “a variable with a value is a counter”, “a condition for executing a cycle” and the third parameter is increasing or decreasing the counter.

The syntax of the For loop inside a For loop:

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

An example of nested For loop:

<?php
int $i , $j;
for( $i=1; $i<3; $i++ )
{
   for( $j=1; $j<3; $j++)
   {
      echo ‘i am programmer  ‘;
   }
   echo ‘<br />’;
}
?>
Выдает :
i am programmer i am programmer
i am programmer i am programmer

Foreach nested loops

The Foreach loop works with arrays and is often used to work with a database.
Foreach syntax inside foreach loop:
foreach(array as value)
{
   foreach(array as value)
   {
      Statement;
   }
   Statement;
}
Example of the Foreach loop:
<?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.

Leave a Reply

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