Follow The New Tutorial updates on WhatsApp, Telegram or YouTube.

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.

Syntax

while(condition){  
//code to be executed  
}  

Alternative Syntax

while(condition):  
//code to be executed  
  
endwhile;  

PHP While Loop Flowchart

PHP While Loop Flowchart

Example

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