PHP - Loops

    PHP - Loops

    Earlier we learned about conditional statements where we run some set of instructions for a particular satisfying condition but the set of code will be run only once. In case we want to loop the execution of the set of codes whenever a condition is true then we use loops. This loop will run until and unless a condition is met. This will help you to save time and avoid writing large codes with different conditions. PHP supports below looping methods.

    • For loop
    • While loop
    • Do-while loop
    • Foreach loop

    There are two other statements that are used to control the flow of this loop statements.

    • Break statement
    • Continue statement

    1. For Loop

    You can use this loop when you know how many times you have to execute the block of code. You can define the number of iterations in advance within the for-loop condition. These are an entry-controlled loop and has three parameters- initialization, a condition that needs to be checked, and the incremental expression. This loop is controlled by the variable value which is defined in the first parameter, whose condition will be checked according to the defined condition as the second parameter and the variable gets incremented or decremented according to the specified expression in the third parameter. All three parameters are required when you are working with a for-loop. If any of the parameters are missing it will throw an error. Syntax for(initialization; condition to be checked; increment value) { //code to be executed } Flowchart Example

    <?php
    for($var=1; $var<=5; $var+=1)
    {
    Echo “<br>”;
    Echo “Hello”;
    }
    Echo “<br>”;
    Echo “Bye”;
    >?

    Output Hello Hello Hello Hello Hello Bye The above loop will be executed until the value of $var is less than or equal to 5. Each time the loop will execute the break condition and print “Hello”. Once the condition fails to meet then the control gets out of the for loop and execute the immediate instruction and print bye.

    2. While loop

    While loop executes the particular set of instructions until a specified condition is true. This loop takes only one parameter that is the condition and the incremental or decremental expression is defined within the code block. Once the condition fails then the control comes out of the code block. Syntax

    while(if the condition is true)
    {
    //code to be executed
    }

    Flowchart Example

    <?php
    $var=1;
    while($var<=5)
    {
    $var+=1;
    Echo "Hello", "<br>";
    }
    ?>

    Output Hello Hello Hello Hello Hello Unlike for-loop, the variable will be defined first outside the while loop and then the condition will be checked for that variable if the condition is true then the followed code will get executed where we have defined the incremental expression for the variable. Then the hello will be printed until the condition is true.

    3. Do-while loop

    Unlike both for-loop and the while-loop, this loop is exit controlled loop. It means the condition will be checked at the exit of the code block which means that the code will be executed at least once before checking for the condition. Once the control reaches the condition statement, if the condition is true then the code will execute again else the flow will be out of the do-while code block. Syntax-

    do
    {
    //code to be executed
    } while (condition to be checked);

    Flowchart Example

    <?php
    $var=1;
    Do
    {
    Echo “Hello”, “<br>”;
    $var+=1;
    }
    While ($var<=0);
    ?>

    Output Hello In the above example, we have a var defined and initialized as 1. As this loop is exit controlled then the loop will execute at least once irrespective of the false condition. Once the control reaches the condition and it is false then the loop will not execute again. That’s why we get one “Hello” as an output.

    4. Foreach loop

    With the help of this loop, you can easily iterate over arrays. The element of an array can be accessed using the counter of this loop. Syntax

    Foreach (array element value)
    {
    // code to be executed
    }

    Example

    <?php 
        $ar1 = array (40, 50, 60); 
        foreach ($ar1 as $var) {  
            echo "$var", "<br>"; 
        } 
    ?>

    Output 40 50 60 In order to access the array elements, an array has been defined and initialized. Now we access the each element of the array using a variable “var” and printed the output using this variable.

    5. Break statement

    If you want to terminate the execution of loop prematurely then you can use break statement. Break statement gives you complete control to come out of the loop immediately. Flowchart- Example

    <?php
    $var=0;
    While ($var<=5)
    {
    $var+=1;
    If ($var==3)
    Break;
    }
    Echo $var;
    ?>

    Output 3 In the above example, first, the while condition will be checked for the “var” variable. If the condition is true then the code will be executed but once the value of “var” reached 3 then the break statement will execute and the control comes out of the code block and print the value of the “var” which is 3 at that time.

    6. Continue Statement

    This continue statement will halt the current iteration but will not terminate the loop. Once the continue statement is encountered within the code body it will skip the execution of the remaining code and starts the new iteration. Flowchart Example

    <?php
             $arr1 = array( 3, 4, 5);         
             foreach( $arr1 as $var ) {
                if( $var == 3 )continue;
                echo "Value is $var <br />";
             }
          ?>

    Output Value is 4 Value is 5 In the above example, once the value of “var” reaches 3 the continue statement will run and skip the code means the value is 3 will not get printed and the control will move to the next iteration. People are also reading: