Tuesday, March 9, 2021
More

    Php Nested Loop

    Must Read

    Programmerhttp://www.improgrammer.net
    We started this site to inspire young minds to motivate and encourage them towards Programming Language. In this site you will get programming tutorials, tech, programming facts, programming fun and programming blogs.

    In this article, we discuss PHP nested loop. Loop inside a loop is called a nested loop. In PHP allows using one loop inside another loop. The following section shows a few examples to illustrate the concept. nested loop syntax and suitable example areas under…

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

     

    Nested while loop in PHP :

    While loops execute the statement repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the statement, execution will not stop until the end of the iteration. Sometimes, if the while expression evaluates to FALSE from the very beginning, the statement won’t even be run once.

    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 :

    do while loops are very similar to while loops, except the truth expression, is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run whereas it may not necessarily run with a regular while loop.

    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 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, the rest of the loop code is skipped and the 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”.

    Nested Loops Example

    Example

    <?php
    $n=5;
    for($i=1; $i<=$n; $i++)
    {
    for($j=1; $j<=$i; $j++) 
    { echo $i ; 
    } 
    echo "\r\n"; 
    }
    ?>

    Output

    1
    
    22
    
    333
    
    4444
    
    55555
    

    Example

    <?php
    $n=1;
    $row=5;
    for($i=1; $i<=$row; $i++)
    {
    for($j=1; $j<=$i; ++$j)
    {
    echo " ";
    echo $n;
    echo" " ;
    ++$n;
    }
     echo "\r\n";
    }
    ?>

    Output

     1 
    
     2  3 
    
     4  5  6 
    
     7  8  9  10 
    
     11  12  13  14  15 
    

    Example

    <?php
    $n=1;
    $row=5;
    for($i=1; $i<=$row; $i++)
    {
    for($j=5; $j>=$i; $j--)
    {
    echo " ";
    echo $j;
    echo" " ;
    
    }
    echo "\r\n";
    }
    ?>
    

    Output

     5  4  3  2  1 
    
     5  4  3  2 
    
     5  4  3 
    
     5  4 
    
     5 
    

    In conclusion, When you have a loop inside the loop is called as a Nested loop in PHP. We highly recommend using PHP manual.

     

    Latest Articles

    More Recipes Like This