In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in PHP.

There are 3 types of loops present in PHP, they are –

  • while loop
  • for loop
  • do-while loop
  • while loop

The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

while (expression) {
statement(s)
}

The while statement evaluates expression, which must return a Boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Let’s see the implementation of while loop. 

 File – code1.php

<?php
$i=0;
while($i<5){
   echo ‘<br> this is while loop!’;
    $i++;
}
?>
output
  • Nested loops

A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem.

Syntax –

While/do-while/for loop // outer loop
Nested while/do-while/for loop // inner loop
  • Nested while loop

Let’s understand simple concepts of inner loop and outer loop with some examples below.

File – code2.php

<?php
$i=0;
$j=0;
 
//outer loop
while($i<5){
   echo ‘<br> this is while loop!’;
   $j=0;
   // inner loop
    while($j<2){
        echo ‘<br>inner-nested while ‘;
        $j++;
    }
   $i++;
}
?>
output
  • do-while loop

loops through a block of code once, and then repeats the loop as long as the specified condition is true.

The PHP programming language also provides a do-while statement, which can be expressed as follows: Syntax –

do {
statement(s)
} while (expression);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

Let’s see some coding examples of do-while loops…

File – code3.php

<?php
$i=0;
do{
echo ‘atleast one time runs!’;
}
while($i<-2);
?>
output

file – code4.php

<?php
$i=0;
do{
echo ‘<br>hello krishna!<br>’;
echo ‘i value: ‘.$i;
$i++;
}
while($i<3);
?>
output
  • For loop

Loops through a block of code a specified number of times.

The ‘for’ statement provides a compact way to iterate over a range of values. Programmers often refer to it as the “for loop” because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

Syntax –

for (initialization; termination;
increment) {
statement(s)
}

When using this version of the for statement, keep in mind that:

  • The initialization expression initializes the loop; it’s executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Let’s see some coding examples using for loop statement.

File – code5.php

<?php
$b=0;
for($b=0;$b<4;$b++){
    echo “<br>b is: “.$b;
    echo “<br>this is a for loop statement!”;
}
?>
output
  • Nested for loop

Here we can implement nested for loop inside another outer loop. Let’s see the usage below…

File – code6.php

<?php
$i=0;
$j=0;
 
for($i=1;$i<=3;$i++){
    echo ‘<br>outer loop!’;
    for($j=0;$j<2;$j++){
    echo ‘<br>inner loop/nested loop!’;
    }
}
?>
output
  • For-each loop

loops through a block of code for each element in an array. The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

foreach (iterable_expression as $value)
    statement
foreach (iterable_expression as $key => $value)
    statement

The first form traverses the iterable given by iterable_expression. On each iteration, the value of the current element is assigned to $value.

Let’s see the usage below…

File – code7.php

<?php
$osArr=[‘windows’,’Ubuntu’,’kali linux’,’Mac OS’,’ios’,’android’];
foreach($osArr as $i){
    echo ‘<br>’;
    echo $i;
}
?>
output

file – code8.php

<?php
$osArr=[‘bill gates’=>’windows’,’linus torvalds’=>’linux’,’steve jobs’=>’Mac OS’,’google’=>’android’];
foreach($osArr as $i=>$val){
    echo ‘<br>creator name: ‘;
    echo $val;
    echo ‘<br>OS is: ‘;
    echo $i;
   
}
?>
output
  • Break statement

When we working on loops in java, we sometime needs to terminate that loop instantly. In that situation, we can use ‘break’ keyword.

 break ends execution of the current for, foreach, while, do-while or switch structure. break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of.

Let’s see the usage below…

File – code9.php

<?php
$i=0;
for($i=0;$i<3;$i++){
    echo “for loop, i is: “.$i;
    break;
}
// statement ‘break’ for-each loop
$arr=[‘akash’,’abdul’,’radha’];
foreach($arr as $j){
    echo “<br>”;
    echo $j;
    break;
}
?>
output

file – code10.php

<?php
$i=0;
while($i<5){
    echo “this is while loop!”;
    $i++;
    break;
}
// statement ‘break’ do-while loop
$i=0;
do{
    echo ‘<br>this is do-while loop’;
    break;
    $i++;
}
while($i<3);
?>
output
  • Continue statement

The continue statement skips the current iteration of a for, while, or do-while loop. The unlabeled form skips to the end of the innermost loop’s body and evaluates the Boolean expression that controls the loop. continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

Let’s see the usage below…

File – code11.php

<?php
/* write a code, which will addition of all
even numbers from 0-10,escapes odd numbers */
 
$sum=0;
 
for($i=0;$i<10;$i++){
    $val=$i;
    $rem=($val%2);
    if($rem==1){
        // for odd numbers remainder is ‘1’
       continue;
    }
    else{
        $sum=$sum+$val;
    }
}
echo ‘total addition is: ‘.$sum;
?>
output