PHP Lesson 13 – How to use while, Do While loop

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

PHP Lesson 13 - How to use while, Do While loop - PHP Lesson 13 - How to use while, Do While loop - PHP Lesson 13 - How to use while, Do While loop - php do while looping
php do while looping

In some cases, it is necessary to execute a statement until a certain result occurs.
This is also used, for example, to output the contents of databases (eg MySQL) or simply the contents of files. PHP offers the while loop, the for loop, and the foreach loop.

Syntax of the while loop – PHP

The syntax of a while loop is structured as follows:

<?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 this is initialized to the value 0.

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

Everything between the curly braces are 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 counter variable is incremented by the value 1. This increase in value is important so that the condition is eventually no longer met. If the condition is always met, for example, because you forgot to increase the count variable $i, your PHP script will result in an infinite loop and will no longer produce a result. Luckily, after a while, PHP breaks your script.

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

<?Php
$counter1 = 0;
$counter2 = 0;
$min = -20;
$max = 30;
while ($count1 < $max AND $count2 > $min) {
   echo "count1: $count1; count2: $count2 <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 2, the other lowered by the value. The while loop runs as long as $count1 $max and $count2 $min are met.

Loop processes influence by means of break and continue

In PHP, the two useful commands are  break, exist and continue,

By means of break, we can end the loop process in the loop. This can be useful if, for example, we are looking for something. For example, we are looking for a specific user. Then we could loop through all users via a while loop. If we find that we have found the user we are looking for, we will stop the loop by means of break.

A slightly simpler example of break is the following:

<?Php
$max = 30;
$count = 0;
$increment = 2;

while ($count < $max) {
   if ($count == 10) {
      echo "At the number 10 we stop";
      break;
   }
   echo "$count,";
   $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, in other words 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, this time with a for loop:

<?Php
$max = 30;
$count = 0;
$increment = 2;

while ($count < $max) {
   $counter += $increment; // Increases $payer by $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, ie it has the values 0, 2, 4 etc.

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

do-while loop – PHP

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 at least 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 that should either be between 0 and 10 or between 20 and 30. By means of the function rand ($min, $max) we can randomly generate a number between $min and $max. This would look like a while loop like this:

<?Php
$random = rand (0, 30);
while ($random> 10 AND $random <20) {
   $random = rand (0, 30);
}

echo "Our random number: $random";
?>

Using the do-while loop, we save 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";
?>

Granted, as a beginner, the do-while loop looks 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.

About The Author

Related posts

Leave a Reply