PHP While Loop

The while statement executes the statement(s) repeatedly as long as the condition is true. The condition is checked every time at the beginning of the loop. Once the condition gets FALSE, it exits from the body of loop. It should be used if the number of iterations is not known.

The while doesn’t require curly braces if you have one statement in the loop body.

PHP while loop can be used to traverse set of code like for loop. The while loop is also called an Entry control loop because the condition is checked before entering the loop body.


//code to be executed  

Alternative Syntax

//code to be executed  

PHP While Loop Flowchart

PHP While Loop Flowchart


$x = 1;
while($x <= 5) {
  echo "The number is: $x <br>";
PHP While Loop Example Output