Difference between while and do while loops in PHP

Loops are fundamental building blocks in programming. You will use them quite often to execute a statement repeatedly. A loop is controlled by a boolean condition, and it continues to run over and over as long as the condition is true.

The most common usage of loops is iterating over arrays or objects. We’ve seen loops in the same context in many of the tutorials. There are different kinds of loops in PHP, namely, for, foreach, while, and do while.

Some starters are often confused about while and do while because they sound sort of similar. However, they work differently. So in this tutorial, we’ll see the difference between while and do while loops in PHP.

Difference between while and do while loops in PHP – While Loop

While loop checks a boolean condition on entry. If the condition is true, it executes the body. After the first run, it rechecks the condition. The loop continues as long as the condition is true. Here’s the syntax of the while loop.

Syntax

while(condition)
{
   //While loop BODY
}

The condition is a boolean statement that evaluates to either true or false.

Flow Chart

Flow charts are intuitive graphical representations of a code. Here’s the flow chart of a while loop.

Difference between while and do while loops in PHP

Example

Here’s an example of the while loop.

<?php
$i = 0;
 
//Loop as long as $i is less than or equal to 10.
while($i <= 10)
{
    echo $i.PHP_EOL;
    $i++; //Increment $i by 1.
}
 
/*
OUTPUT
0
1
2
3
4
5
6
7
8
9
10
*/
 
?>

The loop stops when $i is greater than 10. That’s how the while loop works. Next, we’ll see the do while loop.

Difference between while and do while loops in PHP – While Loop

Do while loop differs from the while loop because it executes at least once regardless of the boolean condition. After the first run, it checks the condition. If the condition is true, it executes the body, else terminates.

Syntax

Here’s the syntax.

do
{
  //do BODY

}while(condition);

Flow chart

Here’s the flow chart for do while loop.

Difference between while and do while loops in PHP

Example

We’ll redo the same example using do while as we’ve seen in the while loop.

<?php
$i = 0;
 
do
{
    echo $i.PHP_EOL;
    $i++;
}while($i <= 10); //If $i is less than or equal to 10.
 
/*
OUTPUT
0
1
2
3
4
5
6
7
8
9
10
*/
 
?>

Difference between while and do while in PHP

Here’s an overview of the difference between while and do while in PHP.

While LoopDo While Loop
The loop’s body doesn’t execute if the condition is falseThe loop’s body executes at least once regardless of the boolean condition
The loop checks the condition first and is hence known as the entry control loopThe loop checks the condition last and is hence known as the exit control loop
The syntax involves a condition followed by a block of codeThe syntax involves a block of code followed by a condition

When to use do while loop?

So, we are left with a common question: When to use the do while loop? We are aware of the while loop and use it often but the question about do while makes us think for a good use case. As we’ve seen already that the do while loop executes at least once. That’s why we would prefer it in places where we need a variable to be initialized before any loop operation.

A typical use case is asking a user input. Let’s see a similar example. Here we’ll initialize a user variable by taking user input from the console. The condition within the while is a function, user_check that will return false if the user already exists.

Here’s the code.

do
{
    //Take user input from console.
    $user = readline("Enter your name: ");
}while(user_check($user)); //If user already exists then return false and terminate the loop.
 
//OUTPUT: User Susan already exists

Conclusion

We have seen the difference between while and do while in PHP in this article. In summary, do while execute at least once regardless of the boolean condition. On the contrary, the while loop executes only if the boolean condition is true. This is all for this article. We expect you to come back to some really interesting PHP articles.

Want to explore more useful PHP tutorials?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

PHP serialize vs json_encode

How to put values in an associative array into another array in PHP

Tell if an element is present in an associative array PHP


Article Categories

Article Tags

© 2022 Confident.Systems