What are some common pitfalls for beginners when trying to calculate averages in PHP?

One common pitfall for beginners when trying to calculate averages in PHP is forgetting to initialize variables properly before performing calculations. To avoid this issue, make sure to initialize variables to zero before starting the calculation. Additionally, ensure that you are correctly summing up the values before dividing by the total count to calculate the average.

// Initialize variables
$total = 0;
$count = 0;

// Sample array of values
$values = [10, 20, 30, 40, 50];

// Calculate sum of values and count
foreach($values as $value) {
    $total += $value;
    $count++;
}

// Calculate average
$average = $total / $count;

echo "The average is: " . $average;