Do While Loop in PHP
The do-while loop in PHP or any other programming languages is just an opposite of the while loop. Its function is exactly like an ordinary while loop, with only one key difference:
The while loop evaluates the condition before executing the code contained in the loop body. If the condition evaluates to false on the first check, the code inside the loop body will not execute.
The do-while loop, on the other hand, executes the code contained in the body of the loop at least once before evaluating the conditional expression at the bottom of loop. In the do-while loop, the conditional expression evaluates at the end of loop rather than the beginning. Thus, we can define the do-while loop statement as:
The do…while is an exit-controlled loop in PHP which allows you to execute a block of code at least once and then repeatedly execute it as long as a specified condition evaluates to true. This makes it a unique loop as compared to the while loop, where the condition is evaluated before executing the block of code.
You can use do-while loop in PHP program when you want the loop body to execute at least once, regardless of whether the condition evaluates to true or false. For example, it is useful in scenarios such as:
- Validating user input.
- Executing initialization steps before condition checks.
Syntax of do…while Loop in PHP
The basic syntax of the do…while loop in PHP is as follows:
<?php
// Initialization
do {
// Loop body.
// PHP code to be executed at least once.
} while (conditional expression);
?>
In the syntax of do-while loop statement, the initialization part of the loop is executed. This sets the value of loop control variable, which acts as the counter to control the loop.
Next, do is a keyword in PHP and the block of code inside the do block will always execute at least once. After the executing the code or statements inside the do block, the control will transfer to the increment or decrement part. The function of increment or decrement part is to increment or decrement the value of control variable by one.
Next, while is a keyword, and the conditional expression inside the parentheses of while statement is checked. The condition inside do…while statement must be a Boolean expression that produces either true or false.
If the conditional expression evaluates to true, the loop repeats. Otherwise, it terminates, and the control goes to the next statement, which is immediately after the do-while loop.
Note: In the do-while loop, the while statement must be terminated with a semicolon (;).
Flowchart Diagram of do-while Loop in PHP
The execution flowchart for the do-while loop in PHP programming is as shown in the below figure.
Basic Examples of PHP do-while Loop
Example 1: Let’s write a PHP program to print numbers from 1 to 6 using do…while loop.
<?php
// Initialization.
$x = 1; // starting number is 1.
do {
echo $x . " "; // print x value.
$x++; // increment x value by 1.
} while($x <= 6); // This statement will execute as long as $x <= 6.
?>
Output: 1 2 3 4 5 6
In this example, the value of loop control variable $x is initially set to 1. The echo statement inside the do block executes and it displays the value 1.
Next, the expression $x++ will execute and increment the value of x by 1. As a result, the value of $x becomes 2. The conditional boolean expression ($x <= 6) inside the while statement will be evaluated to true. Since this condition is true, the control will go back inside the body of the loop and displays the value 2 of $x.
Then, the expression $x++; increments the value of $x by 1, which becomes 3. Again, the condition inside the while statement evaluates to true. Since 3 is also less than 6, the condition is true and the control once again goes back inside the loop body and displays the value 3.
In this way, this process continues until the value of loop control variable $x is less than or equal to 6. As the value of control variable $x is greater than 6, the condition will be false and the loop will terminate.
Example 2: Let’s write a PHP program in which we will check the condition inside the while loop that fails initially and execute the loop body only once.
<?php
$n = 1;
do {
echo "This is a do while loop.";
$n++;
} while($n > 3);
?>
Output: This is a do while loop.
Example 3: Let’s write a PHP program to iterate elements of an array using do-while loop.
<?php
$fruits = ["Apple", "Banana", "Cherry"];
$i = 0;
do {
echo $fruits[$i] . " ";
$i++;
} while ($i < count($fruits));
?>
Output: Apple Banana Cherry
Example 4: Write a PHP code to calculate the sum of numbers from 1 to 15.
<?php
$n = 15;
$sum = 0;
$i = 1;
do {
$sum += $i;
$i++;
} while ($i <= $n);
echo "The sum of numbers from 1 to $n is: $sum";
?>
Output: The sum of numbers from 1 to 15 is: 120
Example 5: Let’s write a PHP program to display a multiplication table of 5 using do-while loop.
<?php
$number = 5;
$i = 1;
do {
echo "$number x $i = " . ($number * $i) . "\n";
$i++;
} while ($i <= 10);
?>
Output: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Nested do-while Loops in PHP
As nesting of while loop, you can also nest do while loop in PHP. When you place a do-while loop inside another do block, it is called nested do-while loop in PHP. Let’s take an example based on the nested do-while loop.
Example 6:
<?php
echo "Multiplication Table: " . "\n";
$row = 1; // Initialization.
do {
$column = 1;
do {
$x = $row * $column;
echo $x . " ";
$column = $column + 1;
} while($column <= 10);
echo "\n";
$row = $row + 1;
} while($row <= 10);
?>
Output: Multiplication Table: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Example 7: Infinite Loop with Break Condition
<?php
$i = 1;
do {
echo $i . " ";
if ($i == 10) {
break;
}
$i++;
} while (true);
?>
Output: 1 2 3 4 5 6 7 8 9 10
Difference Between while and do-while Loops
Here’s a table highlighting the key differences between the while and do-while loops in PHP:
Feature | while Loop | do-while Loop |
---|---|---|
Definition | A loop that evaluates the conditional expression before executing the code block. | A loop that executes the code block at least once before evaluating the conditional expression. |
Type of Loop | Entry-controlled loop (condition is checked at the beginning). | Exit-controlled loop (condition is checked at the end). |
Execution Guarantee | The code inside the loop body may not execute at all if the condition is initially false. | The code inside the loop body will always execute at least once, even if the condition is false. |
Syntax | <?php | <?php |
Condition Check Timing | Condition is evaluated before the loop body executes. | Condition is evaluated after the loop body executes. |
Usage Scenarios | Used when the condition needs to be checked before executing the code block. | Used when the code block must run at least once before checking the condition. |
Quiz and Exercise: Test Your Knowledge
Q1: What will the following code output?
<?php
$x = 1;
do {
echo $x . " ";
$x++;
} while ($x < 1);
?>
Options:
- (a) 1
- (b) No output
- (c) 1 2
- (d) Error
Answer: (a) 1
Q2: Identify the error in the code below:
<?php
do {
echo "PHP ";
} while ();
?>
Options:
- (a) Missing do block
- (b) Infinite loop
- (c) Missing condition loop
- (d) No output
Answer: (c) Missing condition loop
Exercise 3: Factorial Calculation
Write a PHP program to calculate the factorial of a number using a do…while loop.