Are there any potential pitfalls to be aware of when creating a script for calculating averages in PHP?

One potential pitfall when creating a script for calculating averages in PHP is not properly handling empty arrays or dividing by zero. To avoid this issue, you should check if the array is empty before calculating the average and handle the division by zero case gracefully.

function calculateAverage($numbers) {
    if (empty($numbers)) {
        return 0;
    }
    
    $sum = array_sum($numbers);
    $count = count($numbers);
    
    return $sum / $count;
}

// Example usage
$numbers = [10, 20, 30];
echo calculateAverage($numbers); // Output: 20