CGPA Calculator Hash Generator Bcrypt Hasher Tree Visualizer Barcode Generator Engineering Blog Editorial Standards All Tools Games HTML5 JavaScript PHP MySQL

PHP For Loop

Written by RedoHub Editorial Team · Last updated: August 1, 2026

The for loop is used when you know in advance how many times the script should run. It gathers the initialization, condition, and increment logic into a single line, making your code extremely concise.


The for Loop Syntax

The for loop takes three parameters, separated by semicolons:

for (init counter; test counter; increment counter) {
  // code to be executed for each iteration;
}

Example Breakdown

<?php
    for ($x = 0; $x <= 10; $x++) {
        echo "The number is: $x <br>";
    }
?>
  • $x = 0; — Initializes the loop counter value.
  • $x <= 10; — The loop continues as long as this is true.
  • $x++; — Increases the counter value for each iteration.

Counting Backwards

You can also use the for loop to count down by using a decrement operator.

<?php
    for ($x = 10; $x >= 0; $x--) {
        echo "Countdown: $x <br>";
    }
?>

Loops with Custom Steps

The increment part doesn't have to be $x++. You can increase by any value you wish.

<?php
    for ($x = 0; $x <= 100; $x += 10) {
        echo "Step: $x <br>";
    }
?>
Technical Detail: Any of the three parameters in a for loop can be left empty (e.g., for (;;)), but you must still provide the semicolons.
Pro Tip: Use the for loop whenever the number of iterations is fixed, such as printing the days of a month or headers for a table.

Key Points to Remember

  • The for loop is ideal for a fixed number of repetitions.
  • It combines init, test, and update in one parentheses block.
  • Parameters are separated by semicolons, not commas.
  • You can count up or down and use custom step sizes (e.g., $x += 5).
  • It is one of the most performing and widely used loops in all of programming.

Common Mistakes to Avoid

Off-by-one errors. Using $x <= count($arr) instead of $x < count($arr) reads one index past the end of an array, producing an undefined-index warning.
Forgetting the increment step. Leaving the third expression empty (or updating the wrong variable) means the condition never becomes false — an infinite loop that will eventually hit PHP's execution time limit and fatal-error out.
Recalculating an expensive value every iteration. Calling count($arr) directly inside the loop condition re-runs it every pass. Store it in a variable first if the array won't change size during the loop.

Try It: Sum of Even Numbers

A second example that does real work inside the loop instead of just printing values:

<?php
    $sum = 0;
    for ($x = 2; $x <= 20; $x += 2) {
        $sum += $x;
    }
    echo "Sum of even numbers 1-20: $sum"; // 110
?>

Frequently Asked Questions

Q: What's the difference between for and foreach?

A: for is best with a numeric counter and a known range; foreach is designed specifically for iterating over arrays without manually tracking an index.

Q: Can I loop through a string with a for loop?

A: Yes — use strlen($str) for the bound and $str[$i] to access each character by position.

Q: What happens if I write an infinite for loop in PHP?

A: The script keeps running until it hits the server's max_execution_time setting, then PHP fatally errors out. See the PHP manual's for loop reference for the exact evaluation order.