In this article, you will learn about PHP loops and the changes made to the loops in PHP 8. So, first of all, learn the definition of loop.

What is a loop?

A loop is an iteration of code that can be infinite or finite. A loop may consist of a starting point and an ending condition. In PHP we have 4 types of major loops. After this section, you will learn them in the sequence in this PHP 8 tutorial.

Types of PHP loops

  • While – executes a block of code again and again as long as the specified condition becomes true.
  • Do While – executes a block of code and then repeat the loop as long as the specified condition becomes true.
  • For – In this type of loop, a number of iterations are defined to execute the block of code.
  • Foreach – Generally used for arrays and objects and executes a block of code for each element of the array or for each object.

Let’s explore the above four types of loops in PHP one by one.

WHILE LOOP in PHP 8

While loop executes a block of code as long as the specified condition becomes true. If you don’t know, how the condition is specified in the while loop, look at the following syntax of the while loop.

while (expr)
    statement

while (condition is true) {
  code to be executed;
}

The interesting thing about the while loop is that it tells the PHP not to stop the loop until the expression passed in the while is not true. This means, even the condition becomes true inside the while loop or in the statements of the while loop, the loop will not stop at this point. It will complete its iteration and then checks the expression passed in the while loop. So, as a summary of this description, to stop the while loop, the expression checks the condition every time after a cycle.

<?php

$i = 1;
while ($i <= 10) {
    echo $i++;
}


$i = 1;
while ($i <= 10):
    echo $i;
    $i++;
endwhile;
?>

There are two while loops in the above example. They both produce the same result. In the first case, post-increment is used and echo at the same time. The printed value is before the increment. In the next case, the statement is split and EndWhile is used to tell the while loop to continue to the next cycle.

Note: If the expression specified for the while loop does not meet at the first time, the loop will not even run for once.

Examples of the PHP WHILE loop

<?php
$a = 0;

while($a <= 9) {
  echo "The number is: $a <br>";
  $a++;
}
?>

Explanation of example

  • The variable $a starts at 0.
  • Tell the loop, not to stop as long as the $a becomes greater than or equal to 9.
  • Increment the $a value by one on each iteration.

Let’s go through another example

<?php
$a = 1;

while($a <= 100) {
  echo "The number is: $a <br>";
  $a+ = 10;
}
?>

Explanation of example

  • The variable $a start with 0.
  • Tell the loop, not to stop as long as the $a becomes greater than or equal to 10.
  • Increment the $a value by 10 on each iteration.

Do While Loop

It is Just like the while loop with a little difference. Do while loop always executes the block of code once and then checks the condition. If the specified condition meets, the loop will not execute the DO block. Otherwise, it will execute the DO block and then checks the While condition again.

do {
  code to be executed;
} while (condition);

Example of DO WHILE LOOP in PHP

<?php
$a = 0;

do {
  echo "The number is: $a <br>";
  $a++;
} while ($a <= 9);
?>

Explanation of example

  • The variable $a start with 0.
  • Execute the DO block that first prints the number $a and then increment by one.
  • Check if the $a value is greater than or equal to 9.

Let’s see another example, in which we will see the while condition become true within the Do block. The loop will run at least once because the stopping statement is checked after each iteration. Unlike the While loop in which the stopping statement is checked at the beginning.

<?php
$a = 7;

do {
  echo "The number is: $a <br>";
  $a++;
} while ($x <= 6);
?>

Explanation of example

  • The variable $a start with 7.
  • Execute the DO block that first prints the number $a and then increment by one.
  • Check if the $a value is greater than or equal to 6. Which is in fact, was true.

Note: Break statement can be used to stop the While, Do While, For, and Foreach loops in PHP. The code after break statement will not execute even if the loop stop condition is not met. Similarly, if the return statement is used inside the loop, it will stop the loop execution and return.

FOR LOOP in PHP 8

For loop is different from while and do-while loop. It executes the block of code a specified number of times.

for (int counter; check counter; increment or decrement counter) {
  code block to be executed;
}

The first parameter of FOR loop contains the starting value of the counter. The second parameter checks the condition if the counter is at its stopping point. The third parameter increment or decrement the counter on each iteration.

Look at the following examples.

<?php
for ($a = 0; $a <= 10; $a++) {
  echo "The number is: $a <br>";
}
?>

Explanation of example

  • Start the FOR loop and initialize the variable $a with 0.
  • Executes the code block until the $a becomes less than or equal to 10.
  • Increment the $a value by one for each iteration. The loop will execute the code block 11 times. You must dry run the above example code for your better understanding.

PHP Foreach LOOP

The Foreach loop executes the block of code for every element of the array or the object.

foreach ($array as $var) {
  code block to be executed;
}

Example of Foreach loop in PHP 8

<?php
$months = array("Jan", "Feb", "Mar", "Apr");

foreach ($months as $month) {
  echo "$month <br>";
}
?>

The above loop starts by pointing at the first element of the array through the $month variable and goes to the end of the array.

Associative array in Foreach Loop

<?php
$age = array("Jawad"=>"23", "Ahmad"=>"24", "Summi"=>"39");

foreach($age as $a => $val) {
  echo "$a = $val<br>";
}
?>

In the above example, the Forach loop iterates for each key and value of the associative array. We have the key in the $a variable and $val contains its value.

What is the Continue statement in PHP Loops?

Continue statement skip the iteration and statements ahead of Continue and start the next iteration of the loop. Its use case can be to skip some statements or skip specific iteration of the loop.

For complete reference to PHP 8 loops, click here.