A loop is used to execute a block of code repeatedly.
The while loop repeats a block as long as
its condition is true. It is useful when you do not know
in advance exactly how many repetitions will be needed.
The while loop checks its condition
before executing the loop body.
Syntax:
while (condition) {
// Code to repeat
}
If the condition is true, the loop body executes. When the condition becomes false, the loop stops.
Example:
<?php
$i = 1;
while ($i <= 5) {
echo $i . "<br>";
$i++;
}
?>
Output:
1
2
3
4
5
A while loop usually has three important parts:
<?php
$i = 1; // Initialization
while ($i <= 3) { // Condition
echo $i . "<br>";
$i++; // Update
}
?>
Output:
1
2
3
After displaying 3, the value becomes 4. The
condition $i <= 3 is then false,
so the loop terminates.
You can use a while loop to count backward by decreasing the variable in each iteration.
<?php
$i = 5;
while ($i >= 1) {
echo $i . "<br>";
$i--;
}
?>
Output:
5
4
3
2
1
The -- operator decreases the value
of $i by one each time.
A while loop can calculate the sum of a sequence of numbers.
<?php
$i = 1;
$sum = 0;
while ($i <= 5) {
$sum = $sum + $i;
$i++;
}
echo "Sum = " . $sum;
?>
Output:
Sum = 15
The loop adds 1, 2, 3, 4 and 5 to
$sum. The final total is 15.
The modulus operator (%) can help
identify even numbers inside a loop.
<?php
$i = 1;
while ($i <= 10) {
if ($i % 2 == 0) {
echo $i . "<br>";
}
$i++;
}
?>
Output:
2
4
6
8
10
The condition $i % 2 == 0 is true
when the number is even.
You can generate a multiplication table using a while loop.
<?php
$number = 5;
$i = 1;
while ($i <= 10) {
echo $number . " x " . $i . " = ";
echo ($number * $i) . "<br>";
$i++;
}
?>
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Change $number = 5 to any other
number to generate a different table.
You can use a while loop to access array elements by their index.
<?php
$fruits = ["Apple", "Banana", "Mango"];
$i = 0;
while ($i < count($fruits)) {
echo $fruits[$i] . "<br>";
$i++;
}
?>
Output:
Apple
Banana
Mango
Array indexes start at 0 in this example.
The loop stops when $i reaches
the number of elements in the array.
An infinite loop occurs when the condition never becomes false. This often happens when the loop variable is not updated correctly.
Example of a problem:
<?php
$i = 1;
while ($i <= 5) {
echo $i;
// Missing $i++ causes an infinite loop
}
?>
Do not run this example as written: it will keep
printing 1 because the value of $i
never changes.
Corrected version:
<?php
$i = 1;
while ($i <= 5) {
echo $i . "<br>";
$i++;
}
?>
Output:
1
2
3
4
5
The break statement stops a loop.
The continue statement skips the
remaining statements in the current iteration
and moves to the next iteration.
Example using break:
<?php
$i = 1;
while ($i <= 10) {
if ($i == 5) {
break;
}
echo $i . "<br>";
$i++;
}
?>
Output:
1
2
3
4
Example using continue:
<?php
$i = 0;
while ($i < 5) {
$i++;
if ($i == 3) {
continue;
}
echo $i . "<br>";
}
?>
Output:
1
2
4
5
In the second example, 3 is skipped. The counter
is updated before continue, so the
loop can still progress and eventually terminate.
Write PHP programs to complete these exercises:
Sample starting code:
<?php
$i = 1;
$sum = 0;
while ($i <= 10) {
$sum += $i;
$i++;
}
echo "Total = " . $sum;
?>
Expected output:
Total = 55
1. When does a while loop check its condition?
2. Which statement immediately exits a loop?
3. What is a common cause of an infinite while loop?