Home  >  Article  >  Backend Development  >  PHP Do While Loop

PHP Do While Loop

王林
王林Original
2024-08-29 12:41:27622browse

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!";
?>
Note: 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);
Note: ‘do… while loop’ always executes a block of code minimum for a single time while ‘while loop’ does not execute even for a single time. It is because the parameter gets tested after executing the entire block of code.

PHP Do While Loop

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:

PHP Do While Loop

Explanation:

  1. This is the standard opening tag defined for php language
  2. A value of 7 is allocated to the php variable at the start
  3. ‘do…while loop’ started here
  4. Herewith opening curly braces [{] the php ‘do…while loop’ gets the start
  5. Here all the statements inside the ‘do…while loop’ will be executed
  6. php variable value is increased by ‘1’ and the loop continues to execute statements until it turns true.
  7. Herewith closing curly braces [}], the php ‘do…while loop’ ends
  8. The condition is tested here
  9. 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:

PHP Do While Loop

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:

PHP Do While Loop

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:

PHP Do While Loop

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:

PHP Do While Loop

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.

The above is the detailed content of PHP Do While Loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:For Loop in PHPNext article:For Loop in PHP