PHP Loops

The PHP loops are iterative control structures that execute the same block again and again till a specific condition is satisfied.

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 loops in other programming languages, a loop in PHP is useful to execute a statement or a block of statements multiple times until some condition becomes true. It helps the user to save both time and effort to repeatedly write the same lines of code.

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 be executed. The for loop repeats a particular block of code as long as a specific condition is satisfied. 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 the counter value. If the statement is true loop body is executed, and loop variables get updated. Steps are repeated till the exit condition is reached.

  • 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 id1

The number id2

The number id3

The number id4

The number id5

The number id6

The number id7

The number id8

The number id9

The number id10

The number id11

The number id12

The number id13

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 returned 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, a semicolon is used at the end of the loop.

For each loop

The for-each loop is useful to loop over arrays. Please read our article on the  PHP foreach loop for detailed information.

Syntax

foreach(array_element as value){

     //code to be executed

}

There is one more syntax 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($superhero 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. They are really useful for reducing time and minimizing errors. If you are still unclear about some of the other basic concepts of PHP programming, you can read through our previous blogs. Happy Learning!

Leave a Reply

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

Read previous post:
top 25 html interview questions
Top 25 HTML Interview Questions and Answers for Experienced (PDF Free Download)

Are you looking for a job for a web designer and looking for Top HTML Interview Questions? If yes, you...

Close