Is it more efficient to calculate the end price within the loop or store it in an array for later summation?

When calculating the end price of multiple items within a loop, it is more efficient to store the individual prices in an array and then sum them up outside the loop. This approach reduces the number of calculations needed within the loop and simplifies the code logic.

// Example code snippet
$prices = [10, 20, 30, 40, 50];
$total = 0;

foreach ($prices as $price) {
    // Perform any necessary calculations within the loop
    // For example, applying discounts or taxes
    $total += $price;
}

echo "Total price: $total";