Types of All Loop Constructs in PHP

Types of All Loop Constructs in PHP

If you learning PHP and having the problem in understanding loops then this blog would be very beneficial to you.  As you all know that Loops are very important for any programming Language. A loop is a construct that causes a block of code to be repeated a certain number of times. The repetition continues while the condition set for the loop remains true, once the condition becomes false, the loop ends and the control is passed to the statement following the loop.
Three main looping constructs in PHP are:

  1. while loop
  2. do while loop
  3. for loop
  4. foreach

The while Loop in PHP

While loops are the simplest type of loop in PHP.The while loop continues until the evaluating condition become false. The evaluating condition has to be a logical expression and must return either a true or false value. The variable that has been checked in the Boolean expression is called the loop control variable.

Logic behind the {while loop} in PHP

while loop in php

Syntax of {while loop} in PHP

While(expression)
{
     Statements;
}

Example of {while loop} in PHP

1. In the code below, i is the iteration variable; In the while loop the value of i is displayed and incremented with every iteration.

example 1



<?php

$i = 1;

while  ($i <= 10)
{
   echo $i++;    // Value would be displayed before increment
}
?>

example 2


<?php
$i = 1;
while ($i <=10 )
{
  echo $i;
  i++;
}
?>

Output: 0 1 2 3 4 5 6 7 8 9 10 

The do…while Loop in PHP

The do…while looping construct is similar to the while loop construct as both iterates until the specified condition becomes false. It differs from the while loop, in that, the body of the loop is executed at least once, and the condition is evaluated for subsequent executions.

Logic chart of {do…while} loop in PHP

do while loop in php

Syntax of {do…while} loop in PHP

do
{
 Statements;
} while (expression)

Example of {do….while} loop in PHP

1.  The loop below only once because the given logical expression is false.

<?php
   $i = 0;
     do 
{
   echo $i;
   $i++;
    } while ($i >10);
?>

Output:

0 1 2 3 4 5 6 7 8 9 10

The for Loop

The for loop constructs provides a compact way of specifying the statements that control the repetition of steps within the loop. In the for construct, the loop control statement are not within the body of the loop; instead they are written at the top. This makes keyword ‘for’ followed by the parenthesis containing three expressions, each separated by the semicolon. These are initialization expression, and the increment decrement expression.

Syntax of {for loop} in PHP

for ( initialization_expr; test_expr; change_expr)
{
            Statements;
}

Initialization Expression:

The initialization expression is executed only once, when the control is passed to the loop for the first time. It gives the loop variable the initial value.

Test Expression:

The condition is executed each time the control passes to the beginning of the loop. The body of the loop is executed only after the condition has been checked. If the condition evaluates to true, the loop is executed, otherwise the control passes to the statement following the body of the loop.The Increment/ Decrement Expression:

The increment/decrement expression is always executed when the control returns to the beginning of the loop.

The sequence of execution of a complete for loop construct is illustrated in the following figure:

for loop in php

Example of {for loop} in PHP

Value of iteration variable I will be displayed till it is no longer smaller than or equal to 10 in the below example 1. The same is done in example two but this time with the help of a break statement instead of a logical expression.

<?php
      /*example 1*/
      $i = 1;
      for ($i = 1; $i <= 10; $i++)
       {
         echo $i;
     }

   

/*example 2*/


    for ($i = 1; ; $i++ ){
            if($i > 10)
            {
              break;
           }
           echo $i;
    }
?>

The foreach Loop

The foreach loop works only on arrays and will throw an error when you use it with variables of different data types. This loop is used to loop through each index of an array

Syntax:

foreach ($array as $value)
{
            Statements;
}

In above syntax the value of current array element will be assigned to $value variable with every loop iteration.

Example of {foreach loop} in PHP

In the code given below, an array $arr is declared and initialized. Foreach loop is used to fetch each value of $arr in $value with each iteration and double that value.

<?php
      $arr = array(1,2,3,4);
     Foreach ($arr as $value){
            $value = $value * 2;
    }
    // $arr is  now array(2,4,6,8)
   unset($value);  // break the reference with the last element
?>

The Break and Continue Statements in PHP Loops

At times, there is a need to exit a loop before a loop condition is re-evaluated after iteration. Similar in all loops a break statement is used. In addition, the continue statement is used to skip all the subsequent instructions and take the control back to loop.

Break Statement Logic in PHP Loops

break statement in php

Continue Statement Logic in PHP Loops
 

continue statement in php loop

About Author
Hey Friends! I am Harshit pursuing Web Master Course from ADMEC Multimedia Institute.
Presently Mr. Ravi Bhaduria is teaching me PHP, yesterday he taught me about different kinds of loops in PHP and gave me an assignment that was to write an article on loops in PHP. This is the best I can give from all that I have learned.
Thanks for Reading.

Leave a Reply

You must be logged in to post a comment.

Copy link