Updated March 16, 2023
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.
Syntax
<?php
//statements to be executed
echo "This is my first php program!";
?>
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);
Working of PHP Do While Loop
Let’s have a look at the demonstration of one example line by line.
Code:
<?php
$x=7;
do
{
echo "The expected output is: $x<br>";
$x++;
}
while($x<=6)
?>
Output:
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.
Examples of PHP Do While Loop
Below are the examples mentioned:
Example #1
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.
Code:
<html>
<body>
<?php
$n=0;
do{
echo "$n<br/>";
$n++;
}while($n<=9);
?>
</body>
</html>
Output:
Example #2
Code:
<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:
Example #3
Code:
<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:
Example #4
Now we will see the php program of printing a table of 10.
Code:
<?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:
Explanation
The above is 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 First, 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, syntax, flowchart and working along with examples. You may also look at the following article to learn more –