Home » Programming » PHP » Php Do While

How to Use the PHP ‘do while’ Loop, With Examples

This tutorial will teach you how to use the do while statement to build loops in the PHP programming language, and show some examples.

The do while loop is one of the simplest types of loop in PHP. The code inside the loop will execute for as long as the do while condition is true. Change the condition, and the loop will exit.

do while loops are a fundamental part of PHP (and loops are an essential part of computer programming in general) – so it’s good to get to know how they work and what they can be used for.

Loops are commonly used for iterating through arrays of values, counting, and retrying actions until they succeed. Loops are also commonly used for processing queues.

What is a do while Loop?

do while loop will execute a block of code while a given condition is true. When that condition changes to false, the loop will exit, and the code following it will continue executing.

It is similar to the while loop, except that the condition which causes the loop to repeat or exit is checked at the end of the loop rather than at the beginning.

As the conditions which determine whether the loop should continue or not are checked at the end of the loop, the code in the loop will always run at least once.

PHP do while Loop Syntax

The PHP do while loop has the following syntax:

do {
    STATEMENTS
} while (CONDITION);

Note that:

  • CONDITION can be any PHP expression
    • Unlike while loops, in a do while the condition appears at, and is checked at, the end of the loop, rather than the beginning
    • This makes the do while loop behave slightly differently – The STATEMENTS inside the loop will always execute at least once
  • STATEMENTS can be any number of lines of PHP code that will be executed every time the loop repeats
  • The do while loop will execute the STATEMENTS so long as the CONDITION expression evaluates as true or truthy.
    • So, to break out of the do while loop, your STATEMENTS will need to change something in the CONDITION so that it can no longer be considered true.

PHP do while Loop Examples

Below are some example do while loops. Notice that the statements inside each loop will at some point alter the condition used for the while loop so that the loop is exited:

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

Above, a do while loop is executed with the condition that the variable $i is less than or equal to 5. The code statements executed by the loop increment the value of $i after printing it – so that eventually, the condition which exits the loop is met.

As the condition is checked at the end of the loop, even if the initial value of $i is greater than 5, the loop will run once and print the initial value of $i and increment it.

Watch Out for Infinite Loops!

If your loop never reaches a condition that will cause it to exit, your loop will run indefinitely, freezing your application! This is called an infinite loop and should be avoided at all costs!

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment