# PHP While Loop

PHP While Loop is a loop statement which can be used in various forms to execute a block of code continuously as long as the condition of the loop is true, and stops only when the condition fails. These are:

While Loop :
While loop is used to execute a group of action as long as the specified condition is true.

Syntax:

while (condition) { code to be executed if the condition is true; }

Nested While Loop :
Nested While loop is While loop inside a While loop and so on which is used to run multiple loop conditions.
Syntax:

while (condition) { while (condition) { code to be executed if the condition is true; } }

Example 1: Example of While loop

<!DOCTYPE html> <html> <body>   <?php while($i <= 20) { echo "The number is:$i <br>"; i++; } ?>   </body> </html>

Output

The number is: 0 The number is: 1 The number is: 2 The number is: 3 The number is: 4 The number is: 5 The number is: 6 The number is: 7 The number is: 8 The number is: 9 The number is: 10 The number is: 11 The number is: 12 The number is: 13 The number is: 14 The number is: 15 The number is: 16 The number is: 17 The number is: 18 The number is: 19 The number is: 20

Example 2: Example of Nested While loop

<!DOCTYPE html> <html> <body>   <?php $i =0; while($i <= 2) { $j =0; while($j <= 5) { $sum =$i +$j; echo "$sum<br>"; $j++; } echo "<br>";$i++; } ?>   </body> </html>

Output

0 1 2 3 4 5   1 2 3 4 5 6   2 3 4 5 6 7