Lesson 16 of 50 – PHP For Loop
32%

PHP For Loop

A for loop is used to execute a block of code repeatedly. It is especially useful when you know how many times you want to repeat an operation.

1. What Is a For Loop?

A for loop contains initialization, a condition, and an update expression.

Syntax:

for (initialization; condition; update) {
    // Code to repeat
}
  • Initialization: Runs once at the beginning.
  • Condition: Checked before every iteration.
  • Update: Runs after each iteration.

Example:

<?php
for ($i = 1; $i <= 5; $i++) {
    echo $i . "<br>";
}
?>

Output:

1
2
3
4
5
2. Understanding the Execution Order

The initialization runs first. PHP then checks the condition. If it is true, the loop body runs, followed by the update expression.

<?php
for ($i = 1; $i <= 3; $i++) {
    echo "Number: " . $i . "<br>";
}
?>

Output:

Number: 1
Number: 2
Number: 3

After the third iteration, $i becomes 4. The condition is false, so the loop stops.

3. Printing Numbers in Reverse

You can print numbers in descending order by decreasing the counter with the decrement operator.

<?php
for ($i = 10; $i >= 1; $i--) {
    echo $i . "<br>";
}
?>

Output:

10
9
8
7
6
5
4
3
2
1

The loop begins at 10 and subtracts one after each iteration until the counter reaches 0.

4. Displaying Even and Odd Numbers

The modulus operator (%) returns the remainder after division. An even integer has a remainder of zero when divided by 2.

Example: Even numbers

<?php
for ($i = 2; $i <= 20; $i += 2) {
    echo $i . "<br>";
}
?>

Output:

2
4
6
8
10
12
14
16
18
20

Example: Odd numbers

<?php
for ($i = 1; $i <= 10; $i += 2) {
    echo $i . "<br>";
}
?>

Output:

1
3
5
7
9
5. Calculating the Sum of Numbers

A for loop can add a sequence of numbers and store the total in a variable.

<?php
$sum = 0;

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

echo "Sum = " . $sum;
?>

Output:

Sum = 55

The loop adds every integer from 1 through 10 to the $sum variable.

6. Multiplication Table Using For Loop

A for loop makes it easy to generate a multiplication table.

<?php
$number = 6;

for ($i = 1; $i <= 10; $i++) {
    echo $number . " x " . $i . " = ";
    echo ($number * $i) . "<br>";
}
?>

Output:

6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

Change $number = 6 to another number to generate a different multiplication table.

7. Nested For Loops

A nested for loop is a for loop inside another for loop. It is useful for patterns and tables.

<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        echo "* ";
    }

    echo "<br>";
}
?>

Output:

* * *
* * *
* * *

The outer loop controls the rows, while the inner loop controls the stars in each row.

8. Using For Loop with an Array

You can use a for loop to access indexed array elements using their positions.

<?php
$fruits = ["Apple", "Banana", "Mango", "Orange"];

for ($i = 0; $i < count($fruits); $i++) {
    echo $fruits[$i] . "<br>";
}
?>

Output:

Apple
Banana
Mango
Orange

The first array element is at index 0. The condition ensures that the loop stops when all elements have been displayed.

9. Using break and continue

The break statement exits the loop. The continue statement skips the remaining statements in the current iteration.

Example using break:

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i == 5) {
        break;
    }

    echo $i . "<br>";
}
?>

Output:

1
2
3
4

Example using continue:

<?php
for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        continue;
    }

    echo $i . "<br>";
}
?>

Output:

1
2
4
5

The first loop stops when the counter reaches 5. The second loop skips 3 and displays the other numbers.

📝 Practical Assignment

Write PHP programs to complete these exercises:

  1. Print numbers from 1 to 50.
  2. Print numbers from 50 down to 1.
  3. Display even numbers from 1 to 100.
  4. Display odd numbers from 1 to 100.
  5. Calculate the sum of numbers from 1 to 100.
  6. Print the multiplication table of 9.
  7. Calculate the factorial of a positive integer.
  8. Print a square pattern of stars using nested loops.
  9. Display all elements of an indexed array.
  10. Use break to stop the loop at 7.
  11. Use continue to skip the number 5.

Sample starting code:

<?php
$sum = 0;

for ($i = 1; $i <= 100; $i++) {
    $sum += $i;
}

echo "Total = " . $sum;
?>

Expected output:

Total = 5050

🧠 Quick Quiz

1. Which part of a for loop executes only once at the beginning?




2. What is the output of this code?

for ($i = 1; $i <= 3; $i++) {
    echo $i;
}



3. Which statement immediately exits a loop?