PHP Program to Calculate the Average of an Array of Numbers

PHP

The average or mean of an array is a basic mathematical operation in programming. The average is used in analytics, finance, and reporting. One of the real-life applications is a website that calculates the average rating of a product or the average score of a test. In this article, we are going to learn how we can calculate the average of an array of numbers in PHP using different approaches.

What is Average?

The average is obtained by calculating the sum of all elements in an array and then dividing the sum by the total number of elements in the array.

Formula: Average = (Sum of all elements) / (Total number of elements)

Average Calculation Example Array: [2, 4, 6, 8] Sum: 2 + 4 + 6 + 8 = 20 Count: 4 elements Average: 20 รท 4 = 5

Using Built-in Functions

PHP has built-in functions that allow direct calculation of averages. The array_sum() function computes the sum of elements, while count() returns the number of elements

<?php
// Define an array
$numbers = [1, 2, 3, 4, 5];

// Calculate the sum of all elements in the array
$sum = array_sum($numbers);

// Find the total count of elements in the array
$count = count($numbers);

// Calculate the average
$average = $sum / $count;

// Output
echo "The average of the array is: $average";
?>
The average of the array is: 3

Using Loop

In this approach, we iterate through the array using a loop to find the sum of all elements, then calculate the average

<?php
// Define an array
$numbers = [1, 2, 3, 4, 5];

// Initialize a variable to store the sum
$sum = 0;

// Total count of elements in the array
$count = count($numbers);

// Loop through each element in the array to calculate the sum
foreach ($numbers as $num) {
    $sum += $num; // Add each element to the sum
}

// Calculate the average
$average = $sum / $count;

// Output
echo "The average of the array is: $average";
?>
The average of the array is: 3

Using Array Reduce Function

This approach uses PHP's array_reduce() function to calculate the sum of array elements and then divide by the count

<?php
// Define an array
$numbers = [2, 4, 6, 8, 10];

// Calculate the sum using array_reduce()
$sum = array_reduce($numbers, function($carry, $item) {
    return $carry + $item;
}, 0);

// Calculate the average by dividing the sum by the total number of elements
$average = $sum / count($numbers);

// Output
echo "The average of the array is: $average";
?>
The average of the array is: 6

Comparison

Method Time Complexity Space Complexity Best For
Built-in Functions O(n) O(1) Simple, readable code
Loop O(n) O(1) Learning fundamentals
Array Reduce O(n) O(1) Functional programming

Real-life Applications

  • Statistical analysis to find mean values of datasets
  • Grading systems to calculate average marks of students
  • Financial applications to calculate average expenses
  • Sports analytics to determine average player performance

Conclusion

The built-in functions approach (array_sum() and count()) is the most efficient and readable method for calculating array averages in PHP. Use loops when you need more control over the calculation process.

Updated on: 2026-03-15T10:46:19+05:30

852 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements