Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

PHP do While Loop


In these previous tutorials, we have discussed For loop and While loop . In this tutorial, we will discuss do while loop. The do while loop is slightly different from the for loop and while loop. In the while loop compiler checks the condition (expression) first and later executes the code of the block but in the do while loop, the first code executes once, then the expression is checked. The do-while loop is another conditional structure that is used differently in PHP.

Definition of do while loop

In do while loop,code executes at least once then repeat the loop as long as a condition is true.
In the while loop compiler checks the condition then executes the code of block and In do-while loop compile checks the statement then check the condition.

In simple words, in while loop condition writes before the code of block and in the do-while loop first, we write do and after code of the block. We write while condition after the code of the block. That is known as the do-while loop.

Syntax

do
 {
 code to be executed; 
}
while (condition);

In the above syntax, you can see that the code of block is executed once then checks the condition. In the while loop, while condition comes before the block of code but in the do-while loop, while condition comes after the code of block end.

Examples of do while loop

Let's create an example. In the following example, we create a table using do while loop. The same table is created using for loop and while loop.

Create a table

<?php 
$i=1;
do
	//First Print the statement 
{
	echo $i."
"; $i++; } //after statement check the condition while($i<=20); ?>
Run

In the above example, we created a variable. Assign that variable to an initial value 1. With the help of the do-while loop, create a table by incrementing the value of the variable.

If else and break statement inside do while loop

Advance programming developers keep doing new code experiments. Here, you can create a block of code with the help of if else statements and break statement .There are good examples for any programmer . To do experiments, you can also combine all the loops and conditional structure together for effective coding. In the given example, we use the if else statement and break statement inside of the do while loop. In the middle of code blocks, by encapsulating them with do-while (0), and using the break statement. Let's create a variable and use do-while loop.

Example

<?php 
$x=1;
do {
	
    if ($x < 5) {
        echo "X is not big enough";
        break;
    }
	else {
		echo "X is big enough";
        break;
	}
}

 while (0);

?>

Output

X is not big enough

In the above example, we create a variable and gave it a value. After creating variable we create the if condition inside the do-while. If the condition is true then the if block code will be executed and if the condition is false then the compiler breaks the block. And else the statement is executed.


Please Share

Recommended Posts:-