PHP The while loop statement

While loop is the most simple loop in PHP to use, at the beginning of the while loop, It evaluates the conditional expression similar to the if statement and executes the subsequent code block written inside the loop. After the code execution, the control is again passed to the beginning of the while loop, and PHP checks the result of the conditional expression again. If it is true, then the same process is followed. If it is false loop is terminated, and control is passed to the following lines of the PHP code.

			

While ( The conditional expression)
{
	//Execution of the code.
}

				

Here is an example of code that prints the incremental value of variable $var.

			

<?php
$var = 1;
While ( $var < 5 )
{
	echo "The variable value is " . $var . "</br>";
}
?>

				

Output:

The variable value is 1 The variable value is 2 The variable value is 3 The variable value is 4

We often use a do-while loop when we are unsure about the number of iterations required to get the desired result. For example, when you read a file, you need to check that the end of the file has been reached.

Do…while is similar to the while loop, except it, checks the condition at the end, not at the beginning of the loop.