PHP For Loop: Counter Iteration, Step Increments & Nested Loops

When you know in advance exactly how many times a code block needs to execute, the for loop is the most concise, organized, and widely-used iteration structure in PHP. Unlike the while loop where initialization, condition, and increment statements are scattered across multiple lines, the for loop packs all three control mechanisms into a single, clean header line.

Syntax and Architecture of the For Loop

for (initialization; condition; increment/decrement) {
    // Code to be executed on each iteration
}
  • Initialization: Executed only once at the very beginning of the loop to initialize counter variables.
  • Condition: Evaluated before every iteration. If true, the loop body executes; if false, the loop terminates immediately.
  • Increment / Step: Executed after every iteration to advance the counter variable.
<?php
    // Counting from 1 to 5
    for ($i = 1; $i <= 5; $i++) {
        echo "Step: " . $i . "<br>";
    }

    // Stepping backwards (Countdown)
    for ($cd = 5; $cd >= 1; $cd--) {
        echo "Countdown: " . $cd . "...<br>";
    }
    echo "Blastoff!<br>";

    // Stepping by increments of 5
    for ($step = 0; $step <= 25; $step += 5) {
        echo $step . " "; // 0 5 10 15 20 25
    }
?>

Nested For Loops: Multi-Dimensional Matrices & Patterns

In Computer Science and BCA university practicals, nested loops are frequently used to print geometric star patterns and coordinate grids. In a nested loop, for every single iteration of the outer loop, the inner loop executes its complete cycle:

<?php
    // Printing a 5-row Star Pyramid Pattern
    $rows = 5;

    for ($i = 1; $i <= $rows; $i++) {
        // Inner loop prints stars equal to current row number
        for ($j = 1; $j <= $i; $j++) {
            echo "* ";
        }
        echo "<br>";
    }
?>

Output Rendered:

*
* *
* * *
* * * *
* * * * *

Real-World Example: Rendering Dynamic HTML Tables

For loops excel at generating dynamic HTML table rows and grid cells:

<?php
    echo "<table border='1' cellpadding='8' style='border-collapse:collapse;'>";
    echo "<tr style='background:#4F5D95; color:#fff;'><th>Number</th><th>Square (n²)</th><th>Cube (n³)</th></tr>";

    for ($n = 1; $n <= 5; $n++) {
        echo "<tr>";
        echo "<td>" . $n . "</td>";
        echo "<td>" . ($n ** 2) . "</td>";
        echo "<td>" . ($n ** 3) . "</td>";
        echo "</tr>";
    }

    echo "</table>";
?>

BCA / CS University Exam Corner

High-Frequency Exam Question:

Q: What happens if all three expressions are omitted: for ( ; ; ) { ... }?
A: In PHP, for ( ; ; ) creates an intentional infinite loop because the missing test condition implicitly evaluates to true. The only way to exit such a loop is by executing an explicit break; statement inside the loop body.

Interactive Student Practice Session

Exercise 17.1: Write a nested for loop to print an inverted right-angled triangle of numbers (5 down to 1).

Exercise 17.2: Write a for loop that calculates the Factorial of a given integer (e.g. 5! = 5 × 4 × 3 × 2 × 1 = 120).

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!