PHP loops: while, do-while, for and foreach explained

PHP loops: while, do-while, for and foreach explained

In this article you will learn how to repeat a series of actions using loops in PHP.

The different types of loop in PHP

Loops are used to run the same block of code over and over, for as long as a given condition holds. The basic idea of a loop is to automate repetitive tasks in a program and save time and effort. PHP supports four different types of loop.

  • while – loops over a block of code for as long as the given condition is true.
  • do while – the block of code runs once, then the condition is evaluated. If the condition is true, the statement is repeated for as long as the given condition stays true.
  • for – loops over a block of code until the counter reaches a given number.
  • foreach – loops over a block of code for each item of an array.

By the end of this chapter you will also know how to walk through the values of an array with the php foreach loop. The php foreach loop works with arrays specifically.

PHP while loop

The php while statement loops over a block of code for as long as the condition given in the php while statement evaluates to true.

php while

php
while(condition){
    // Code to run
}

The example below sets up a loop that starts with $i=1. The loop keeps running for as long as $i is lower than or equal to 3. $i goes up by 1 on every pass through the loop:

php while

php
$i = 1;
while($i <= 3){
    $i++;
    echo "Le nombre est " . $i . "<br>";
}

Result:

Le nombre est 2
Le nombre est 3
Le nombre est 4

PHP do while loop

The php do while loop is a variation on the while loop, which evaluates the condition at the end of each iteration. With a php do while loop, the block of code runs once, then the condition is evaluated. If the condition is true, the statement is repeated for as long as the given condition evaluates to true.

php while

php
do{
    // Code to run
}
while(condition);

The next example sets up a loop that starts with $i=1. It then increases $i by 1 and prints the output. The condition is evaluated afterwards, and the loop keeps running for as long as $i is lower than or equal to 3.

php do while

php
$i = 1;
do{
    $i++;
    echo "The number is " . $i . "<br>";
}
while($i <= 3);

Result:

Le nombre est 2
Le nombre est 3
Le nombre est 4

Difference between the while and do while loops

The php while loop differs from the php do while loop on one important point: with a php while loop, the condition is tested at the start of each iteration, so if the conditional expression is false the loop never runs at all.

With a php do while loop, on the other hand, the loop always runs once, even when the conditional expression is false, because the condition is evaluated at the end of the iteration rather than at the start.

PHP for loop

The php for loop repeats a block of code for as long as a given condition holds. It is usually used to run a block of code a set number of times.

php for

php
for(initialisation; condition; incrementation){
    // Code to run
}

The parameters of the php for loop mean the following:

  • initialisation – used to initialise the counter variables, evaluated once and unconditionally before the first pass through the body of the loop.
  • condition – the condition is evaluated at the start of each iteration. If it evaluates to true, the loop carries on and the nested statements run. If it evaluates to false, the loop stops.
  • increment – updates the loop counter with a new value. It is evaluated at the end of each iteration.

The example below sets up a loop that starts with $i=1. The loop carries on until $i is lower than or equal to 3. The $i variable goes up by 1 on every pass through the loop:

php for

php
for($i=1; $i<=3; $i++){
    echo "Le nombre est " . $i . "<br>";
}

Result:

Le nombre est 2
Le nombre est 3
Le nombre est 4

PHP foreach loop

The php foreach loop is used to iterate over arrays.

php foreach array

php
foreach($array as $value){
    // Code to run
}

The next example shows a loop that prints the values of the given array:

php foreach array

php
$colors = array("Rouge", "Vert", "Bleu");

// Loop over the colours array
foreach($colors as $value){
    echo $value . "<br>";
}

Result:

Rouge
Vert
Bleu

There is another php foreach syntax, an extension of the first one.

php foreach key value

php
foreach($array as $key => $value){
    // Code to run
}

php foreach key value

php
$superhero = array(
    "nom" => "Peter Parker",
    "email" => "peterparker@marvel.com",
    "âge" => 16
);

// Loop over the superhero array
foreach($superhero as $key => $value){
    echo $key . " : " . $value . "<br>";
}

Result:

nom : Peter Parker
email : peterparker@mail.com
âge : 18

Conclusion

You should now have a good grip on loops. If you are after other articles to sharpen your PHP, have a look at my article on creating and formatting dates in PHP and at my PHP tutorial on Laravel, which gives you the basics for working with a PHP framework.

PHP

Damien Flandrin Web developer since 2010, creator of Gekkode and Email Impact. Every article is tested on a real project before publication. Contact
Newsletter

New tests, tutorials and projects, by e-mail.

Reproducible tests, versioned code, dated results. Never any spam.