Difference between while & do-while loops: 3 PHP Code Examples (2023)

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

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

Using While vs Do-While Loops in PHP

In this article, we have seen the difference between while and do while in PHP. 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.

Learn More About Loops in PHP

Want to learn how to use loops in PHP? This article is part of our series on looping through PHP arrays. Check out our full series to become a PHP array loop ninja.

Learn the Fundamentals of Good Web Development

Please take a moment and sign up for our free email course on the fundamentals of good web development. Every week is packed with a roundup of articles on our site and from around the web, where we go deep into developing exceptional web applications. We have meetups, code reviews, slack chats, and more.

Click here to get started

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.