Introduction to PHP Do While Loop
PHP –Hypertext Pre-processor
A server-side scripting language, PHP is a very popular and widely used open-source language. Initially, PHP was known as –Personal Home Page. In this topic, we are going to learn about PHP Do While Loop.
PHP Syntax
<?php
//statements to be executed
echo “This is my first php program!”;
?>
Every statement in PHP ends with a semicolon (;). This technically conveys to the PHP engine that this is going to be the end of the statement. And, then the engine moves to the next line and executes the code till semicolon (;).
PHP Loops
In certain situations, we have to use the same block of code many times. In this case, one can make use of loops. Instead of using almost equal code for almost the same conditions, you can run a block of code over and o by using loops.
#Following are some of the PHP looping statements.
- while: a block of code runs as far as the provided condition is ‘T.rue’
- do…while: a block of code runs at least for once and repeats the same code as far as the provided condition is ‘True.’
- for: a block of code runs for the provided number of times
- foreach: a block of code runs for each element in an array
PHP ‘do…while loop.’
After having an understanding of ‘while… Loop’, the next step is to understand the logic of ‘do…while loop’. Unless the stated condition is ‘True’, this ‘do…while loop’ can be executed repeatedly.
A small difference between ‘while’ and ‘do…while’ lop is the place where the condition meets its validation point in ‘while loop’ the condition is tested before executing any statement in the block of code, i.e. at the beginning. And, ‘do…while loop’ the condition is tested once, after executing the statements in the block code, then the same processes recur until it is true.
Technically, it can be explained as ‘do…while loop’ is always completed solitary execution, then test suggested condition, and keep on repeating the same block of code while the stated condition stands ‘True’.
Syntax of ‘do…while.’
do{
//code/statements to be executed
}while(condition is true);
Let’s have a look at the demonstration of one example line by line.

4.5 (9,000 ratings)
View Course
Example:
- <?php
- $x=7;
- do
- {
- echo “The expected output is: $x<br>”;
- $x++;
- }
- while($x<=6)
- ?>
Output:
The expected output is: 7
Explanation:
- This is the standard opening tag defined for php language
- A value of 7 is allocated to the php variable at the start
- ‘do…while loop’ started here
- Herewith opening curly braces [{] the php ‘do…while loop’ gets the start
- Here all the statements inside the ‘do…while loop’ will be executed
- php variable value is increased by ‘1’ and the loop continues to execute statements until it turns true.
- Herewith closing curly braces [}], the php ‘do…while loop’ ends
- The condition is tested here
- php closing tag
I hope you understood the details working on the above example.
Now, we will see some more examples for better understanding.
Let’s see a very basic example of printing numbers ‘0 to 9’. With this example, you will be able to write the program for squares of numbers or multiples of a number, etc., just by changing the condition.
Example:
<html>
<body>
<?php
$n=0;
do{
echo "$n<br/>";
$n++;
}while($n<=9);
?>
</body>
</html>
Output:
0
1
2
3
4
5
6
7
8
9
Example:
<html>
<body>
<?php
$x0=0;
do {
echo "Executed Statement: $x0 <br />";
echo "this execution is done after the above statement ‘$x0’ is printed <br />";
$x0=$x0+1;
}while ($x0<=5)
?>
</body>
</html>
Output:
Executed Statement: 0
this execution is done after the above statement ‘0’ is printed
Executed Statement: 1
this execution is done after the above statement ‘1’ is printed
Executed Statement: 2
this execution is done after the above statement ‘2’ is printed
Executed Statement: 3
this execution is done after the above statement ‘3’ is printed
Executed Statement: 4
this execution is done after the above statement ‘4’ is printed
Executed Statement: 5
this execution is done after the above statement ‘5’ is printed
Example:
<html>
<body>
<?php
$BookPrice = 15;
do {
echo "The book price is " . $BookPrice . ". Students can buy this book. <br>";
$BookPrice = $BookPrice + 1;
}
while ($BookPrice <= 10);
echo "The book price is " . $BookPrice . ". Student cannot afford this costly book!";
?>
</body>
</html>
Output:
The book price is 15. Students can buy this book.
The book price is 16. Students cannot afford this costly book!
Now we will see the php program of printing a table of 10.
Example:
<?php
@$tab=$_GET['tab'];
$i=1;
do
{
$t=$tab*$i;
echo $t." ";
$i++;
}
while ($i<=10);
?>
<body>
<form>
Enter Your table<input type="text" name="tab"><br/>
<input type="submit" value="Table">
</form>
</body>
Output:
10 20 30 40 50 60 70 80 90 100
Enter your table
Explanation
The above is an example is slightly different. We have made use of one text-box and one button using an HTML script. The main logical part is performed inside the php script.
Very firstly, we have collected the value entered by the user by $_GET.
Variable $i holds value 1.
And, here the logic is applied inside the php code to print the table of 10.
Conclusion
In the above article, we have come up with essential points on PHP loops and learned about the various types. Specifically, we have learned PHP ‘do…while loop’ in detail. This article gives information about do…while loop, it’s working, and its use with examples. The functioning of the ‘do…while loop’ is very easy to understand.
To summarize, PHP ‘do…while loop’ eliminates the need for executing a similar task again and again. So, If you want to do reduce the workload on PHP language, make use of ‘do…while loop’ frequently.
Recommended Articles
This is a guide to PHP Do While Loop. Here we discuss the information about do…while loop, it’s working, and its use along with examples. You may also look at the following article to learn more –