PHP While Loop

In this tutorial, you will learn how to use While loop in PHP to execute a block of code repeatedly with the help of examples.


The PHP While Loop executes a block of code repeatedly until the condition specified in while the condition is true. It has two parts, a control statement and a body of a loop.

The control statement in the while loop consists of some condition and direct the body of the loop to execute until the specified condition becomes false.

PHP While Loop Syntax

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

Another Syntax

while(condition): 
//code to be executed 

endwhile;

PHP While Loop Example

<?php 
$num=1; 
while($num<=10){ 
echo "$num<br/>"; 
$num++; 
} 
?>

Output

1
2
3
4
5
6
7
8
9
10

Alternative Example

<?php
$num=1;
while($num<=10):
echo "$num<br/>";
$num++;
endwhile;
?>

Output

1
2
3
4
5
6
7
8
9
10

Please get connected & share!

Advertisement