PHP while loop

5 min read

In some cases, it is necessary to execute a statement until a specified result occurs.

This is also used, for example, to output the contents of databases (e.g., MySQL) or merely the text contents of files. PHP offers the while loop, the for loop, and the foreach loop.

<?php 
 while  (condition)  {   
     instructions 
 } 
 ?> 

An example would be:

<?php
$i = 0; while ($i <10) {
echo "$i,";
$i++;
}
?>

In the above example, a counter variable is defined first, in this case, the variable $i and it's initialized with the value 0.

The condition of the while loop is $i <10, that is, we check whether the variable has a value less than 10.

Everything between the curly braces is the instructions of the loop. These are repeated as often as the condition is met. In the above example, the variable $ i is output and then the count variable is incremented by the value 1. This increase in value is necessary so that the condition is eventually no longer met.

If a condition is always satisfied, for example, because you forgot to increment the count variable $i, your PHP script will result in an infinite loop and will no longer produce the expected results. Fortunately, PHP breaks off after a certain period of your script.

By the way, you can make the condition as well as the instructions within the loop arbitrarily more complexs. For example, you can define more than one counter variable:

<?php
$counter1 = 0;
$counter2 = 0;
$min = -20;
$max = 30;
while ($counter1 < $max AND $counter2 > $min) {
echo "counter1: $counter1; counter2: $counter2 <br>";
$counter1 += 2; // Abbreviated notation for $counter1 = $counter1 + 2;
$counter2 -= 3; // Abbreviated notation for $count2 = $count2-3;
}
?>

In the above example, two variables are defined $ counter1 and $counter2. The one variable is increased by the value of 2, and the other lowered by the same value. The while loop runs as long as $count1 < $max and $count2 > $min are satisfied.

Breaking loop processes by using the break and continue commands

Using break command, we can force to finish looping process in a loop. This can be useful if, for example, we are looking for something. For instance, we are looking for a specific user. Then we could loop through all users via a while loop. If we have seen the user we are looking for, we will stop the loop using break.

A slightly simpler example of break is the following:

<?php
$max = 30;
$counter = 0;
$increment = 2;
while ($counter < $max) {
if ($counter == 10) {
echo "at the number 10 we stop";
break;
}
echo "$counter,";
$counter += $increment; // increases the $count by the value $increment
}
?>

This code counts up to a maximum of 30 and always increments the $count by the value $increment, i.e., by the value 2. However, if the variable $count should take the value 10, the loop is terminated.

continue does not finish the entire loop, but skips the rest of the loop body. The following example illustrates this, with a for loop:

<?php
$max = 30;
$count = 0;
$increment = 2;
while ($count < $max) {
$count += $increment; // increases the $ payer by the value $increment
if ($count >= 10 AND $count <= 15) {
echo "A number between 10 and 15 <br>";
continue;
}
echo "$count <br> <br>";
}
?>

This loop starts at 0 and counts up to the value of $max. The $counter is always incremented by the value $increment, i.e., it has the values ​​0, 2, 4, etc.

If the $count has a value between 10 and 15, a text will be output and via continue the further instructions in the loop body will be skipped. On the output, we see that the numbers 10, 12 and 14 are not displayed, but we have only three times the text there, that we have caught a number between 10 and 15.

do-while loop

A small modification of the while loop is the do-while loop , In this case, the condition of the loop is checked only after the loop body. This means that the loop body will go through at least once.

The syntax of the do-while loop looks like this:

<?php 
 do  {   
     instructions 
 }  while ( condition ) ;       
 ?>

Suppose we want to generate a random number between 0 and 10 or between 20 and 30. Using the function rand ($ min, $ max), we can randomly generate a number between $min and $max. This would look like a while loop:

<?php
$random = rand(0, 30);
while ($random > 10 AND $random < 20) {
$random = rand(0, 30);
}
echo "Our random number: $random";
?>

Using a do-while loop, we save ourselves the initialization of $random, because the loop body is executed before the condition is checked. The above example would look like this:

<?php
do{$random = rand (0, 30); }
while ($random > 10 AND $random <20);
echo "Our random number: $random";
?>

As a beginner, the do-while loop may look a bit confusing. If you have not fully understood the purpose, then this is not a problem. Do-while loops are rarely used in practice.

« »