What are the best practices for summing up values in a loop in PHP to generate a total sum at the end?

When summing up values in a loop in PHP to generate a total sum at the end, it is best to initialize the total sum variable outside of the loop, then continuously add each value to the total sum inside the loop. Finally, after the loop has completed, the total sum will contain the sum of all the values.

// Initialize total sum variable
$totalSum = 0;

// Loop through values and add them to total sum
foreach ($values as $value) {
    $totalSum += $value;
}

// Output total sum
echo "Total sum: " . $totalSum;