PHP While Loop
PHP while loop can be used to traverse set of code like for loop. The while loop executes a block of code
repeatedly until the condition is FALSE. 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 loop is also called an Entry control loop because the condition is checked before entering the
loop body. This means that first the condition is checked. If the condition is true, the block of code will
be executed.
Syntax
while(condition){
//code to be executed
Alternative Syntax
while(condition):
//code to be executed
endwhile;
PHP While Loop Flowchart
flowchart of php while loop
PHP While Loop Example
<?php
$n=1;
while($n<=10){
echo "$n<br/>";
$n++;
}
?>
Output:
10
Alternative Example
<?php
$n=1;
while($n<=10):
echo "$n<br/>";
$n++;
endwhile;
?>
Output:
1
10
Example
Below is the example of printing alphabets using while loop.
<?php
$i = 'A';
while ($i < 'H') {
echo $i;
$i++;
echo "</br>";
?>
Output:
A
B
PHP Nested While Loop
We can use while loop inside another while loop in PHP, it is known as nested while loop.
In case of inner or nested while loop, nested while loop is executed fully for one outer while loop. If
outer while loop is to be executed for 3 times and nested while loop for 3 times, nested while loop will
be executed 9 times (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer
loop).
Example
<?php
$i=1;
while($i<=3){
$j=1;
while($j<=3){
echo "$i $j<br/>";
$j++;
$i++;
?>
Output:
11
12
13
21
22
23
31
32
33
PHP do-while loop
PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-while loop is
guaranteed to run at least once.
The PHP do-while loop is used to execute a set of code of the program several times. If you have to
execute the loop at least once and the number of iterations is not even fixed, it is recommended to use
the do-while loop.
It executes the code at least one time always because the condition is checked after executing the code.
The do-while loop is very much similar to the while loop except the condition check. The main difference
between both loops is that while loop checks the condition at the beginning, whereas do-while loop
checks the condition at the end of the loop.
Play Video
Syntax
do{
//code to be executed
}while(condition);
Flowchart
flowchart of php do while loop
Example
<?php
$n=1;
do{
echo "$n<br/>";
$n++;
}while($n<=10);
?>
Output:
10
Example
A semicolon is used to terminate the do-while loop. If you don't use a semicolon after the do-while loop,
it is must that the program should not contain any other statements after the do-while loop. In this case,
it will not generate any error.
<?php
$x = 5;
do {
echo "Welcome to javatpoint! </br>";
$x++;
} while ($x < 10);
?>
Output:
Welcome to javatpoint!
Welcome to javatpoint!
Welcome to javatpoint!
Welcome to javatpoint!
Welcome to javatpoint!
Example
The following example will increment the value of $x at least once. Because the given condition is false.
<?php
$x = 1;
do {
echo "1 is not greater than 10.";
echo "</br>";
$x++;
} while ($x > 10);
echo $x;
?>
Output:
1 is not greater than 10.