The while loop in PHP
What is while loop?
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.
See also: the for loop
Structure of while loop
Following is the general structure of using the while loop in PHP programs:
1234567 while ( condition/expression ){//Statements to be executed here;endwhile;}
As expression is evaluated at the beginning of while loop, if the condition is false upfront, the statements inside the curly braces will not execute. In order to execute the block of code at least once, even the condition is false, you may use the do..while loop.
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.
See online demo and code
This is how the while loop is used in above example:
1 2 3 4 5 6 7 8 9 |
$x=1; while ($x<=5){ echo $x."<br>"; $x++; } |
Using the decrement operator in while loop example
In this example, the value of the variable is decremented in each iteration and displayed in an HTML div.
See online demo and code
The following code for while loop is used in this example:
1 2 3 4 5 6 7 8 9 |
$x=5; while($x>=1){ echo $x."<br>"; $x--; } |
Using endwhile keyword in while loop
You may use the endwhile keyword as well in the while loop. This example will output the same as in the first example:
See online demo and code
This is how the endwhile is used in above example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php //A demo of PHP whileloop $x = 1; while ($x <= 5): echo $x . "<br>"; $x++; endwhile; ?> |
A demo of incrementing by more than one in 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.
See online demo and code
The following code is executed:
1 2 3 4 5 6 7 8 9 |
$x = 10; while ($x <= 50): echo $x . "<br>"; $x = $x + 10; endwhile; |
Using while loop with PHP arrays
You may use the PHP while loop with 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.
See online demo and code
This is how array elements are displayed by using the while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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++; } ?> |
An example of using while loop with associative array
In this example, an associative array of three elements is created. A while loop is used with key/value where index and values of elements are displayed.
See online demo and code
First, an associative array is created as follows:
1 2 3 4 5 6 7 8 9 |
$arr_courses = array( "course1" => "PHP", "course2" => "MySQL", "course3" => "Java", ); |
After that the while loop is used:
1 2 3 4 5 |
while (list($key, $value) = each($arr_courses)) { echo $key .' = '.$value ."<BR>"; } |