PHP Loop: For, ForEach, While, Do While [Example]

A Loop is an Iterative Control Structure that involves executing the same number of code a number of times until a certain condition is met.

PHP For Loop

The above code outputs โ€œ21 is greater than 7โ€ For loops For… loops execute the block of code a specifiednumber of times. There are basically two types of for loops;

  • for
  • forโ€ฆ each.

Letโ€™s now look at them separately. For loop It has the following basic syntax

<?php
for (initialize; condition; increment){

//code to be executed

}
?>

HERE,

  • โ€œforโ€ฆ{โ€ฆ}โ€ is the loop block
  • โ€œinitializeโ€ usually an integer; it is used to set the counterโ€™s initial value.
  • โ€œconditionโ€ the condition that is evaluated for each php execution. If it evaluates to true, then execution of the for… loop continues. If it evaluates to false, the execution of the for… loop is terminated.
  • โ€œincrementโ€ is used to increment the initial value of counter integer.

How it works

The flowchart shown below illustrates how for loop in php works

PHP For Loop

How to code

The code below uses the โ€œforโ€ฆ loopโ€ to print values of multiplying 10 by 0 through to 10

<?php

for ($i = 0; $i < 10; $i++){

$product = 10 * $i;

echo "The product of 10 * $i is $product <br/>";
}

?>

Output:

The product of 10 x 0 is 0 
The product of 10 x 1 is 10 
The product of 10 x 2 is 20 
The product of 10 x 3 is 30 
The product of 10 x 4 is 40 
The product of 10 x 5 is 50 
The product of 10 x 6 is 60 
The product of 10 x 7 is 70 
The product of 10 x 8 is 80 
The product of 10 x 9 is 90 

PHP For Each loop

The php foreach loop is used to iterate through array values. It has the following basic syntax

<?php
foreach($array_variable  as $array_values){

block of code to be executed

}
?>

HERE,

  • โ€œforeach(โ€ฆ){โ€ฆ}โ€ is the foreach php loop block code
  • โ€œ$array_dataโ€ is the array variable to be looped through
  • โ€œ$array_value โ€œ is the temporary variable that holds the current array item values.
  • โ€œblock of codeโ€ฆโ€ is the piece of code that operates on the array values

How it works The flowchart shown below illustrates how the forโ€ฆ eachโ€ฆ loop works

PHP For Each loop

Practical examples

The code below uses forโ€ฆ each loop to read and print the elements of an array.

<?php

$animals_list = array("Lion","Wolf","Dog","Leopard","Tiger");

foreach($animals_list as $array_values){

echo $array_values . "<br>";

}

?>

Output:

Lion
Wolf
Dog
Leopard
Tiger

Letโ€™s look at another example that loops through an associative array.

An associative array uses alphanumeric words for access keys.

<?php

$persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female");

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

echo "$key is $value"."<br>";

}

?>

The names have been used as array keys and gender as the values.

Output:

Mary is Female
John is Male
Mirriam is Female

While Loop

PHP While loop

They are used to execute a block of code a repeatedly until the set condition gets satisfied

When to use while loops

  • While loops are used to execute a block of code until a certain condition becomes true.
  • You can use a while loop to read records returned from a database.

Types of while loops

  • Doโ€ฆ while – executes the block of code at least once before evaluating the condition
  • Whileโ€ฆ – checks the condition first. If it evaluates to true, the block of code is executed as long as the condition is true. If it evaluates to false, the execution of the while loop is terminated.

While loop

It has the following syntax

<?php
while (condition){

block of code to be executed;

}
?>

HERE,

  • โ€œwhile(โ€ฆ){โ€ฆ}โ€ is the while loop block code
  • โ€œconditionโ€ is the condition to be evaluated by the while loop
  • โ€œblock of codeโ€ฆโ€ is the code to be executed if the condition gets satisfied

How it works

The flow chart shown below illustrates how the whileโ€ฆ loop works

While Loop

Practical example

The code below uses the whileโ€ฆ loop to print numbers 1 to 5.

<?php

$i = 0;

while ($i < 5){

echo $i + 1 . "<br>";

$i++;

}

?>

Output:

1
2
3
4
5

PHP Do While

The difference between Whileโ€ฆ loop and Doโ€ฆ while loop is doโ€ฆ while is executed at-least once before the condition is evaluated.

Letโ€™s now look at the basic syntax of a doโ€ฆ while loop

<?php
do{

block of code to be executed

}
?>

while(condition);

HERE,

  • โ€œdo{โ€ฆ} while(โ€ฆ)โ€ is the doโ€ฆ while loop block code
  • โ€œconditionโ€ is the condition to be evaluated by the while loop
  • โ€œblock of codeโ€ฆโ€ is the code that is executed at least once by the doโ€ฆ while loop

How it works

The flow chart shown below illustrates how the whileโ€ฆ loop works

PHP Do While

Practical example

We are now going to modify the whileโ€ฆ loop example and implement it using the doโ€ฆ while loop and set the counter initial value to 9.

The code below implements the above modified example

<?php

$i = 9;

do{

    echo "$i is"." <br>";

}

while($i < 9);

?>

The above code outputs:

9

Note the above example outputs 9 only.

This is because the doโ€ฆ while loop is executed at least once even if the set condition evaluates to false.

Summary

  • The forโ€ฆ loop is used to execute a block of a specified number of times
  • The foreachโ€ฆ loop is used to loop through arrays
  • Whileโ€ฆ loop is used to execute a block of code as long as the set condition is made to be false
  • The doโ€ฆ while loop is used to execute the block of code at least once then the rest of the execution is dependent on the evaluation of the set condition

Summarize this post with: