PHP Snippets
avg
Information
(PHP Version >= 4.0.4)Functions used in this snippet: is_array, sizeof, array_sum.
Description
int avg( array array )
Takes the array array and gives you the average of its values.
Snippet
<?php
function avg($array){
if(is_array($array) && sizeof($array) !== 0){
return (array_sum($array) / sizeof($array));
}
return false;
}
?>
Example
avg Can be used in the following way:
<?php
$array = array(1,3,4,9,41,5,87,6,62,4,8,6,8,7,5,6);
echo avg($array); // 16.375
?>
0 Responses to avg