PHP –Hypertext Pre-processor
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
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.
Code:
<?php $x=7; do { echo "The expected output is: $x<br>"; $x++; } while($x<=6) ?>
Output:
Explanation:
I hope you understood the details working on the above example.
Now, we will see some more examples for better understanding.
Below are the examples mentioned:
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:
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:
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:
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.
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.
The above is the detailed content of PHP Do While Loop. For more information, please follow other related articles on the PHP Chinese website!