Lesson 14 of 50 – PHP While Loop
28%

PHP While Loop

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.

1. What Is a While Loop?

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
2. Understanding Initialization, Condition and Update

A while loop usually has three important parts:

  • Initialization: Sets the starting value.
  • Condition: Determines whether the loop continues.
  • Update: Changes the value so the loop can eventually stop.
<?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.

3. Printing Numbers in Reverse

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.

4. Calculating the Sum of Numbers

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.

5. Displaying Even Numbers

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.

6. Multiplication Table Using While

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.

7. Using a While Loop with an Array

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.

8. The Infinite While Loop

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
9. Using break and continue

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.

📝 Practical Assignment

Write PHP programs to complete these exercises:

  1. Print numbers from 1 to 20 using a while loop.
  2. Print numbers from 20 down to 1.
  3. Calculate the sum of numbers from 1 to 100.
  4. Display all even numbers from 1 to 50.
  5. Display all odd numbers from 1 to 50.
  6. Print the multiplication table of 7.
  7. Calculate the factorial of a positive integer.
  8. Display every element of an array using a while loop.
  9. Use break to stop printing when the counter reaches 8.
  10. Use continue to skip the number 5.

Sample starting code:

<?php
$i = 1;
$sum = 0;

while ($i <= 10) {
    $sum += $i;
    $i++;
}

echo "Total = " . $sum;
?>

Expected output:

Total = 55

🧠 Quick Quiz

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?