Do While Loop In PHP

This article discusses use of the PHP do while loop with examples, how it differs from other loop types, and how it is used.

Similar to the while loop, a do-while loop executes code at least once, regardless of whether the condition is true or false.

It is useful when you need to run at least once a particular block of code, such as when validating user input or initializing a loop counter.



Php Do While Loop

PHP do while loop will execute the block of code once. The loop will then be checked, and the loop will continue to be repeated as long as the specified condition is true.

Syntax

do
{
The code that needs to be executed;
}
while (condition is true);

As shown in the example below, the variable $a is set to 1 at the beginning ($a = 1).

Once the do while loop has been executed, the program will write some output, and then increment the value of the variable $a by one. Thereafter, the condition is checked to see if $a is less than, or equal to 10, and if it is, the loop will continue to run as long as $a is less than, or equal to 10:

Example: 

<?php $a = 1; do { echo "The number is: $a <br>"; $a++; } while ($a <= 10); ?>

The example below loops from “A” to “H” using a do while loop:

Example: 

<?php $a = "A"; do { echo "$a <br>"; $a++; }while($a <= "H"); ?>
Remember: Once the loop has been completed, the condition is tested in a do…while loop to see if it meets the conditions.
If the conditions are either true or false, then the do-while code will execute the statements at least once no matter what the condition is.

Please see the example below for a better understanding.

As you can see in below example, the $a variable is set to 11, then the loop is run, and then it will check the condition:

Example: 

<?php $a = 11; do { echo "$a <br>"; $a++; } while ($a <= 10); ?>
In the below example for better understanding a “do while“, we use alphabets for printing where the condition is $a <= “H” but the value given in variable $a is “M“:

Example: 

<?php $a = "M"; do { echo "$a <br>"; $a++; }while($a <= "H"); ?>

Example Explanation:

  1. The code demonstrates the use of PHP do while loop to display a sequence of characters.
  2. The code defines a variable $a and assigns it the value “M“.
  3. The code then enters a do-while loop. The loop will always execute at least once, even if the condition in the while statement is not true.
  4. Inside the loop, the echo statement is used to display the current value of $a.
  5. The value of $a is then incremented using the $a++ statement. This means that the next character in the ASCII character set will be displayed in the next iteration of the loop.
  6. The loop continues to execute until the value of $a is greater than “H”.
  7. Since the characters in the ASCII character set are ordered alphabetically, the loop will display the characters “M”, “N”, “O”, “P”, “Q”, “R”, “S”, and “T”, which are all the characters between “M” and “H” inclusive.
  8. Once the value of $a is greater than “H”, the loop terminates, and program execution continues.

So the example demonstrates how to use PHP do while loop to display a sequence of characters, and how to increment a character using the $a++ statement.

Conclusion

PHP do while loop is a powerful programming construct that allows developers to execute a block of code repeatedly based on a specified condition. Its an ideal choice when you want to ensure that a block of code is executed at least once, regardless of whether the condition is true or false.

With the help of PHP do while loop, developers can create complex logic structures, iterate through arrays, and perform other repetitive tasks efficiently and effectively.

By understanding how to use the do-while loop, developers can create robust, efficient code that meets their needs and delivers outstanding results.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *