The PHP do while loop is another pattern for PHP loop, it works like in the PHP while loop but here, it executes one loop before the condition then check the condition if it has a true value it will repeat the do block again otherwise, it will exit the loop.

The basic pattern of do while loop would be like the following syntax.

  do {
    // - statements …
  } while( express )


In the following sections, I will show you some examples with PHP do while loop. Let’s get started.

How the Do While Loop Works in PHP

As you know PHP executes the statements line by line.

The PHP do while has two parts which are : The statement block and the condition block.

do while loop in php

When the PHP interpreter reads the PHP statements it executes the do block part first then executes the condition. So, if it returns true it will do another loop else.

In the following section I will cover that by examples. Let’s see that.


The below code will do increment for the $d variable, if the while condition has false it will exist the loop directly.

   $d = 0;
   do {
   } while ( false );

   echo $d; 


This will only increase the $d variable with 1 and exit the code.

So the output would be 1.

For another example.


 $val = "";
  do { 
    echo "This is valid email";
  } while($val == "");


This code will do an infinity loop because the condition has true Boolean value.

You can use this as infinity loop to check about something if it executed it will exit the loop.

Wrapping Up

In this article, you understood how to use the do while loop and you saw many examples.