Loops in PHP - for, while, do while & foreach

Discussion in 'PHP' started by MinalS, Nov 13, 2015.

  1. We shall understand about the following loops in PHP.
    1. For loop statement
    2. The while loop
    3. The do while loop
    4. The foreach loop
    5. The continue statement
    6. The break statement

    For loop statement



    For loop is used when user is aware the number of items statements inside the block are executed.

    Syntax:

    Code:
    for ( initialization; condition; increment )
    {
        Statements; 
    }
    
    OR
    Code:
         for ( initialization; condition; increment ):
            Statements;
        endfor; 
    
    Example:

    Code:
        <html>
            <body>
            <?php
                $x = 5;
                $y = 7;
                for ( $i = 0; $i < 2; i++ )
                {
                    $x + = 3;
                    $y += 2;
                }
                echo (" The values in variable x and y are x = $x and y = $y" );
            ?>
            </body>
        </html>
    

    The while loop



    In while loop, the statements inside the block are executed till the condition defined inside the expression is true.

    Syntax:

    Code:
    while ( expression )
    {
        Statements;
    }
    
    OR
    Code:
        while ( expression ):
            statements
        endwhile;
    
    Example:

    Code:
    <html>
        <body>
        <?php
            $a = 10;
            while ( $a <= 15 )
                echo $a;
                $a++;
            endwhile;
        ?>
        </body>
    </html>
    

    The do while loop



    In the do while loop, the loop is executed at least once irrespective of the condition defined inside the expression. Later, the loop is executed if the condition is true.

    Syntax:

    Code:
    do
    {
        Statements to be executed;
    } while ( condition );
    
    Example:

    Code:
    <html>
        <body>
            <?php
                $x = 5;
                do
                {
                    echo $x;
                } while ( $x > 5 ) ;
            ?>
        </body>
    </html>
    

    The foreach loop



    The foreach loop is used for arrays and objects. User can easily iterate over the arrays using the foreach loop. User cannot use the loop on other data types.

    Syntax:

    Code:
    foreach ( expression as $value )
        statement
    
    OR
    Code:
    foreach ( expression as $key => $value )
        statement
    
    Example:

    Code:
    <html>
        <body>
        <?php
            $arr1 = array ( 10, 20, 30, 40 );
            foreach ( $arr1 as $value )
            {
                echo "The array contains value as $value";
            }
        ?>
        </body>
    </html>
    

    The continue statement



    The continue statement is used to pause the execution of the PHP statements. The loop is not terminated by the user. The continue statement is added inside the code block. Once the statement is encountered, the remaining statements are skipped.

    Example:

    Code:
    <html>
        <body>
            <?php
                $arr1 = array ( 15,20,25,30 );
                foreach ( $arr1 as $value )
                {
                    if ( $value == 20 )
                        continue;
                    echo "The value is $value";
                }
            ?>
        </body>
    </html>
    

    The break statement



    The statement is used to end the execution of the loop before it is expected to terminate. The statement is added inside the block. User can easily terminate the loop.

    Example:

    Code:
        <html>
            <body>
                <?php
                    $x = 2;
                    while ( $x < 6 )
                    {
                        $x++;
                        if ( $x == 4 ) 
                            break;
                    }
                    echo ("Loop is terminated");
                ?>
            </body>
        </html>
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice