Table of Contents
Introduction
Often, you want the same block of code to run over and over again a certain number of times. Instead of adding the same chunk of code into your script, we can use loops. Loops are used to execute the same block of code again and again, as long as a certain condition is true.
PHP loops come in the following flavors:
Loop Type | Description |
---|---|
while |
Loops through a block of code as long as the specified condition is true. |
do...while |
Loops through a block of code once, and then repeats the loop as long as the specified condition is true. |
for |
Loops through a block of code a specified number of times. |
foreach |
Loops through a block of code for each element in an array. |
We discuss the while
loop in this article and consider the rest in subsequent sections.
Syntax of the while Loop
The while
loop executes a block of code as long as a certain specified condition is true. The syntax of the while
loop is given as:
Syntax
Thewhile
loop.
1while (expression) {
2 statement(s);
3}
- The
while
loop tells PHP to execute the nested statement(s) repeatedly, as long as theexpression
evaluates totrue
. - The value of the
expression
is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration. - If the while
expression
evaluates tofalse
from the very beginning, the nested statement(s) won’t even be run once.
There is also an alternative syntax.
Syntax
Thewhile
loop (alternative syntax).
1while (expression):
2 statement(s);
3endwhile;
where the colon :
and endwhile;
replace the braces.
ADVERTISEMENT
Using the while Loop
Example
Using thewhile
loop.
1<?php
2 $i = 1;
3 while ($i <= 10) {
4 echo $i++; // post-increment
5 }
6?>
12345678910
In the above example, the line echo $i++
prints out the value of $i
before incrementing (see post-increment). In the beginning, it prints out 1
, then $i
increases to 2
and the loop repeats until $i
becomes 10
. The statement echo $i++
then prints out 10
and $i
increases to 11
afterwhich the expression
evaluates to false
and the loop stops.
Example
Using thewhile
loop to generate a 3 times table.
1<?php
2 $count = 1;
3 while ($count <= 10){
4 echo "$count times 3 is " . $count * 3 . "<br>";
5 ++$count;
6 }
7?>
1 times 3 is 3
2 times 3 is 6
3 times 3 is 9
4 times 3 is 12
5 times 3 is 15
6 times 3 is 18
7 times 3 is 21
8 times 3 is 24
9 times 3 is 27
10 times 3 is 30
Note that it is immaterial whether we had used ++$count
or $count++
in the last statement within the loop.
The next example illustrates a shortened version where the use of ++$count
or $count++
will have a bearing on the results.
Example
Using thewhile
loop to generate a 3 times table with ++$count <= 10
.
1<?php
2 $count = 0;
3 while (++$count <= 10)
4 echo "$count times 3 is " . $count * 3 . "<br>";
5?>
1 times 3 is 3
2 times 3 is 6
3 times 3 is 9
4 times 3 is 12
5 times 3 is 15
6 times 3 is 18
7 times 3 is 21
8 times 3 is 24
9 times 3 is 27
10 times 3 is 30
In the above example, $count
starts with the value of 0
. The expression ++$count <= 10
means that PHP first increases the value of $count
by 1 before evaluating the boolean expression.
On the other hand, had we used $count++
, we will need to modify the boolean expression as $count++ <= 9
. This means that PHP first evaluates the boolean expression before increasing the value of $count
by 1.
Example
Using thewhile
loop to generate a 3 times table with $count++ <= 9
.
1<?php
2 $count = 0;
3 while ($count++ <= 9)
4 echo "$count times 3 is " . $count * 3 . "<br>";
5?>
1 times 3 is 3
2 times 3 is 6
3 times 3 is 9
4 times 3 is 12
5 times 3 is 15
6 times 3 is 18
7 times 3 is 21
8 times 3 is 24
9 times 3 is 27
10 times 3 is 30