PHP Loops

The PHP loops are iterative control structures that execute the same block again and again as long as a specific condition becomes true.

Read Also: PHP Switch and Continue

Loops in PHP

This article shows how to use the different loops in PHP. Loops are a key way to control the execution of code.

Like other programming languages, loop in PHP is useful to execute a statement or a block of statement multiple times until some condition becomes true. It helps the user to save both time and effort to write the same code many times.
PHP supports four different types of loops:

  1. For loop
  2. While loop
  3. Do while loop
  4. Foreach loop

Let us see the detailed description of each loop mentioned in the list.

For Loop

For loop is useful when the user knows how many times the block needs to execute. The for loop repeats a particular block of code as long as a specific condition becomes true. It consists of three parameters, namely initialization, test condition, and the counter.

Syntax

for(initialization; condition; increment){
    //code to be executed
}

First, initialize the loop variable to some value, then check whether the variable is less than or greater than counter value. If the statement is true loop body is executed, and loop variables get updated. Steps are repeated till the exit condition comes.

  • Initialization: In this expression, we must initialize the loop variable to some value.
  • Condition: At the starting of each iteration, the condition is evaluated. If it evaluates to true, the loop continues, and if it evaluates to false, the execution of the loop ends.
  • Increment: It updates the loop counter with a new value.

How for loop in PHP works

PHP for loop

Example

<?php 

for($i = 1; $i <= 13; $i++){

    echo “The number id”.$i.”<br>”;

} 

?>

Output

The number is 1

The number is 2

The number id 3

While Loop

The While loop iterates a block of code until some condition becomes true. It is an entry control loop. The loop first checks the condition at the starting of the loop, then if it is true, it executes the block of statements and goes on executing until some condition becomes true. The while loop is useful to read records return from a database query.

Syntax

While(if the condition is true){
    //Code to be executed
}

How while loop works in PHP

PHP while loop

Example

<?php

$num = 2;

while ($num < 12) {

    $num += 2;

    echo $num, "\n";

}

?>

Output

4

6

8

10

12

Do While Loop

The do-while loop is a variant of a while loop. It is an exit control loop. The do-while loop first enters the loop executes the statements, and then checks the condition. A statement executes at least once on using the do-while loop. After executing once, the program executes as long as the condition holds.

Syntax

do{

     //code to be executed

}While(if condition is true);

How Do while works in PHP

PHP Do while loop

Example

<?php

$num = 2;

do {

    $num += 2;

    echo $num, "\n";

} while ($num < 12);

?>

Output

4

6

8

10

12

Difference between while and do-while loop

While loop

Do-while loop

In a while loop, the controlling condition appears at the start of the loop.In the do-while loop, the controlling condition appears at the end of the loop.
The loop never executes if the condition at the first iteration appears false.At least once the loop executes, even if the condition is false at the first iteration.
The while is also known as entry controlled loop.The do-while is also known as an exit controlled loop.
In the while loop, the semicolon is not used.In the do-while loop, semicolon is used at the end of the loop.

For each loop

The for-each loop is useful to loop over arrays

Syntax

foreach(array_element as value){

     //code to be executed

}

There is one more syntax of for each loop which is the extension of the first

foreach($array as $key => $value){

     //code to be executed

}

Example 1

<?php

$colors = array(“Red”,”Green”,”Blue”);

foreach ($colors as $value) {

    echo $value .”<br>”;

}

?>

Output

Red

Green

Blue

Example 2

<?php

$superhero = array (

                         “name” => “Suman”,

                         “email” => “[email protected]”,

                          “age”    => “18”

  );

foreach($superheros as $key => $value){

     echo $key  .” : ” .$value .”<br>”;

}

?>

Output

name: Suman

email: [email protected]

age:18

Read More: PHP Functions

Conclusion

PHP loops are often used while programming web applications. I hope you have a complete understanding of PHP loops.

Leave a Reply

Your email address will not be published. Required fields are marked *

More in PHP Tutorial
switch and continue in PHP
PHP Switch and Continue

In this article, we see how to use the switch case statement in PHP. The switch statement is useful to...

Close