The PHP while loop is used to execute a given block of code until the given expression in the while is false.
This is the simplest type of loop in PHP that works just like C language.
The condition or value of the expression is checked at the beginning of the while loop.
Structure of while loop
Following is the general structure of using the while loop in PHP programs:
while ( condition/expression ){ //Statements to be executed here; endwhile; }
As the expression is evaluated at the beginning of the while loop, if the condition is false upfront, the statements inside the curly braces will not execute.
An example of using the while loop to display numbers
In this simple example of using the while loop in PHP, a variable is assigned an initial value.
The value of the variable is checked in each iteration and incremented by 1 if the condition is true.
The code:
$x=1; while ($x<=5){ echo $x."<br>"; $x++; }
Output:
2
3
4
5
Using the decrement operator in while loop example
In this example, the value of the variable is decremented in each iteration and displayed by echo statement.
$x=5; while($x>=1){ echo $x."<br>"; $x--; }
Output:
4
3
2
1
Using endwhile keyword in the while loop
You may use the endwhile keyword in the while loop. This example will output the same as in the first example.
PHP code with markup:
<!doctype html> <html> <head> <style> div { background: #408080; height: auto; width:200px; border-radius: 15px; padding:20px; font-size:17px; color:#fff; } </style> </head> <body> <div class="fordiv"> The output of while with endwhile:<br /> < ?php //A demo of PHP whileloop $x = 1; while ($x <= 5): echo $x . "<br>"; $x++; endwhile; ?> </div> </body> </html>
Output:
A demo of incrementing by more than one in the while loop
In above examples, the increment or decrement of the variable’s value is 1 by using the ++ or –.
You may specify other values. In this example, the increment in each iteration is 10.
PHP code:
$x = 10; while ($x <= 50): echo $x . "<br>"; $x = $x + 10; endwhile;
Output:
20
30
40
50
Using while loop with PHP arrays
You may use while loop with PHP arrays as well.
- In this example, an array of five elements is created.
- The expression in the while loop is the total length of elements by using the count function.
- So, the while loop will keep on executing until it reaches the maximum length of the array.
- In each iteration, the value of the current element is displayed by using the echo statement of PHP.
Code:
<!doctype html> <html> <head> <style> div { background: #0000A0; height: auto; width:270px; border-radius: 15px; padding:20px; font-size:17px; color:#fff; } </style> </head> <body> <div class="fordiv"> The output of while with array:<br /><br /> < ?php //A demo of PHP whileloop $arr_variable = array (10,20,30,40,50); $x = 0; while ($x<count($arr_variable)){ echo "The array current element value = $arr_variable[$x] <br />"; $x++; } ?> </div> </body> </html>
Output:
An example of using while loop with an associative array
In this example, an associative array of three elements is created. A while loop is used with key/value where the index and values of elements are displayed.
Code:
<!doctype html> <html> <head> <style> div { background: #804040; height: auto; width:270px; border-radius: 15px; padding:20px; font-size:17px; color:#fff; } </style> </head> <body> <div class="fordiv"> The output of while with associative array:<br /><br /> < ?php //A demo of PHP whileloop $arr_courses = array( "course1" => "PHP", "course2" => "MySQL", "course3" => "Java", ); while (list($key, $value) = each($arr_courses)) { echo $key .' = '.$value ."<BR>"; } ?> </div> </body> </html>
Output:
First, an associative array is created as follows:
$arr_courses = array( "course1" => "PHP", "course2" => "MySQL", "course3" => "Java", );
After that, the while loop is used:
while (list($key, $value) = each($arr_courses)) { echo $key .' = '.$value ."<BR>"; }