PHP Loop 1 to 10

PHP Loop 1 to 10

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Summation Trap: Why Your PHP Loop Calculates Ten Times Instead of Accumulating

You are attempting to calculate the sum of integers from 1 to 10, a classic exercise in control flow logic. The expected mathematical result is 55 ($1 + 2 + 3 \dots + 10$). However, your current implementation outputs $90$ ($10 \times 9$) for every iteration where $i equals 10, or simply repeats the same addition logic incorrectly.

The root cause lies entirely within the variable assignment inside your loop body. You have declared a variable named $sul, which likely stands for "sum" or "total," but you are assigning to it an expression that adds the current iteration number $i to itself ten times:

$sul = $i + $i + $i + $i + $i + $i + $i + $i + $i + $i;

This logic is fundamentally flawed for a running total calculation. In this specific line, the variable $sul is never used in the calculation itself; it merely stores the result of adding $i ten times. Furthermore, because you are reassigning $sul in every iteration without carrying over its previous value, the loop does not accumulate results. Even if that line were corrected to use $sul, the logic would still fail to sum 1 through 10 because it adds the current $i a fixed number of times (ten) rather than adding $i once to an existing total.

To achieve a running sum, you must initialize an accumulator variable before the loop begins and then add the current iteration value to that accumulator inside the loop condition. Here is the corrected implementation:

$sum = 0; // Initialize the accumulator before the loop starts

for ($i = 1; $i <= 10; $i++) {
    $sum += $i; // Add the current number to the running total
    echo "$i + " . ($sum - $i) . " ... = $sum<br>";
}

In this corrected version, $sum starts at zero. On the first iteration ($i is 1), the code calculates 0 + 1, resulting in 1. On the second iteration ($i is 2), it calculates 1 + 2, resulting in 3. This pattern continues until $i reaches 10, where the final calculation is the previous sum (54) plus 10, yielding 64... wait, let's re-verify the math. $1+2+3+4+5+6+7+8+9 = 45$. $45 + 10 = 55$. The logic holds.

The operator += is syntactic sugar for "add the right operand to the left operand and assign the result back to the left operand." It is the standard idiom for accumulation in iterative algorithms. Without initializing $sum to zero outside the loop, you would be adding $i to an uninitialized variable, which triggers a warning or treats it as null in loose typing contexts, breaking your calculation immediately.

Optimization Considerations

While the for loop is perfectly adequate for summing 1 to 10, developers often consider alternative approaches for scalability and performance. If you were summing millions of integers, the overhead of a PHP loop becomes negligible compared to the cost of I/O operations like echo. However, if your goal is purely mathematical aggregation without side effects (like printing each step), using built-in functions or functional programming constructs can be more expressive.

For instance, Laravel provides helper methods that abstract these loops away entirely. In a modern PHP application, you might prefer to let the framework handle the iteration if it simplifies the codebase. You could use collect from the Illuminate Collection class to perform a reduction:

use Illuminate\Support\Collection;

$sum = (new Collection(range(1, 10)))->sum();
echo $sum; // Outputs 55

This approach leverages the underlying iterator logic to compute the sum in C-level speed within the collection engine. While range(1, 10) creates an array first, consuming negligible memory for such a small scope, it removes the imperative loop syntax from your PHP code, reducing the chance of off-by-one errors or initialization mistakes.

If you are working on a larger project where this specific calculation is part of a complex data processing pipeline, integrating these utilities ensures consistency across your application. For more details on how to structure efficient data collection and iteration in Laravel contexts, refer to https://laravelcompany.com.

The choice between an explicit for loop and a functional reduction depends on readability and the specific performance constraints of your environment. For learning purposes or small-scale scripts, the corrected for loop remains the most transparent way to understand how accumulation works step-by-step.

Our network