PHP Tutorials

Tutorial 7 - While Loops

Sometimes you want to repeat the same action several times, perhaps with different data. You use something called a loop to achieve this. A loop is simply a way of getting the code inside the loop to execute a number of times.

The first type of loop we are going to learn about in this tutorial is the while loop. This loop repeats while a certain condition is true. There are 2 forms of the while loop. The while condition can appear at the top or bottom of the loop. The syntax of these 2 is slightly different, as we'll see below.

The following example shows a while loop with the while at the top of the loop.


<?php

$i 
0;
$finalVal 5;

while (
$i $finalVal)
{
    echo 
"Iteration $i\n";
    
$i ++;                  // Add 1 to the value of $i
}

echo 
"Final value is $i\n";

?>

If we execute the above code fragment, we will display the following:

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Final value is 5

The code within the loop executes while the condition "($i < 5)" is true. Each time the code within the loop executes, it echos a line, then increments $i by 1. When $i gets to 5, the while condition is false, so the loop finishes and the final value is echoed.

Let's take a look at a while loop with the while at the bottom of the loop.
<?php

$i 
0;
$finalVal 5;

do
{
    echo 
"Iteration $i\n";
    
$i ++;                  // Add 1 to the value of $i
}
while (
$i $finalVal);

echo 
"Final value is $i\n";

?>

Look at the differences between the 2 loop constructs. When we put the while at the bottom of the loop, we introduce the loop with a "do" statement. When the while is above the loop, there is no semi-colon after the while condition. When it is below, there is a semi-colon after the while condition. Adding a semi-colon after the while when it appears before the loop is a common mistake that even experienced programmers encounter. If you do this, the semi-colon marks the end of the while loop, so the part in the curly brackets is not controlled by the while, and will always execute just once.

When we run this code, we get the same output displayed. So why do we have 2 different while loop constructs? There is a difference if the condition is false when the loop is entered. If the while is at the top of the loop, the loop code will not execute. If the while is at the bottom of the loop, the loop code will always execute at least once. Let's illustrate this with an example:
<?php

while (== 2)
{
    echo 
"First loop code executed\n";
}

do
{
    echo 
"Second loop code executed\n";
}
while (
== 2);

?>

When we execute this example, we get the following output:

Second loop code executed

The first loop code does not execute since the condition is false before the loop is started. The second loop code does execute, since the test is done after the loop code is executed.

While loops are not the only loops used in PHP. Another powerful loop, the for loop, is introduced in the next tutorial.