The average (arithmetic mean) of N numbers is their sum divided by the count.
Remember
average = (a₁ + a₂ + … + aₙ) / N
Example: 1, 2, 3, 4, 5
sum = 15, N = 5
average = 15 / 5 = 3
Values
Sum
N
Average
1, 2, 3, 4, 5
15
5
3
10, 15, 20, 25, 30
100
5
20
2, 4
6
2
3
1, 2
3
2
1.5
Approach
How to Find the Average of N Numbers in PHP
Sum every value once, then divide by the count — guard empty input first.
Reject N <= 0 or an empty array (no division by zero).
Iterate the N values (loop or array_sum).
Accumulate a running sum.
Return sum / N as a float.
Method
Idea
Time
Best for
Running sum + N
Loop N times with a fixed list
O(N)
Interviews, demos
Array helper
Sum with foreach, divide by count
O(N)
Reusable default
array_sum / count
Built-in sum and length
O(N)
Concise application code
Pseudocode
Pseudocode
function Average(values):
n = length of values
if n == 0:
return error // or a documented sentinel
sum = 0
for each value in values:
sum = sum + value
return sum / n
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Formula
average = sum / N
Guard
Never divide when N <= 0
PHP float
15 / 5 is 3; 3 / 2 is 1.5
Shortcut
array_sum($a) / count($a)
Not median
Median needs sorting; average does not
1..5
sum 15 → average 3
Try it
Live Preview
Enter comma-separated numbers to see the sum, count, and average.
Comma-separated numbers. Tap a chip or type your own list.
Result
values = [1, 2, 3, 4, 5]
sum = 15, N = 5
average = 3