average in php

To compute the average of a set of numbers in PHP, you can use the array_sum() and count() functions in combination. Here's an example:

main.php
$numbers = array(2, 4, 6, 8, 10);
$sum = array_sum($numbers);
$count = count($numbers);
$average = $sum / $count;
echo "The average is: " . $average;
150 chars
6 lines

In this example, we have an array of numbers and we use array_sum() to calculate the sum of the array. We also use count() to get the number of elements in the array. Then we divide the sum by the count to get the average.

The output of this code will be:

main.php
The average is: 6
18 chars
2 lines

gistlibby LogSnag