What is the best practice for summing temperature values below and above 30°C in PHP?

When summing temperature values below and above 30°C in PHP, it is best to use conditional statements to separate the values into two groups and then calculate the sum for each group separately. This approach ensures that only the relevant values are included in the sum for each category.

// Sample temperature values
$temperatures = [25, 32, 28, 31, 29, 35];

// Initialize sum variables for below and above 30°C
$sumBelow30 = 0;
$sumAbove30 = 0;

// Iterate through the temperatures and calculate the sum for each category
foreach ($temperatures as $temp) {
    if ($temp < 30) {
        $sumBelow30 += $temp;
    } else {
        $sumAbove30 += $temp;
    }
}

// Output the sum for temperatures below and above 30°C
echo "Sum of temperatures below 30°C: " . $sumBelow30 . "\n";
echo "Sum of temperatures above 30°C: " . $sumAbove30 . "\n";