How can one avoid incorrect results when performing calculations within loops in PHP?

When performing calculations within loops in PHP, it's important to initialize any variables outside of the loop to avoid accumulating incorrect results. If a variable is initialized inside the loop, it will be reset to its initial value in each iteration, potentially leading to incorrect calculations. By initializing the variable outside the loop, its value will persist across iterations and produce accurate results.

$total = 0; // Initialize the variable outside the loop

for ($i = 1; $i <= 10; $i++) {
    $total += $i; // Perform calculations within the loop
}

echo $total; // Output the correct total