Simple Averaging

Description: A clearly written function that will get the average of numbers that were passed in.

<?PHP
 
// simple averaging
 
FUNCTION average($numbers) {
   $a = SPLIT(' ', $numbers);
   FOREACH ($a AS $b) { $c = $c + $b; $d++; }
   RETURN NUMBER_FORMAT($c/$d,0);
}
 
// For those that like using explode
 
FUNCTION average($numbers) {
   $a = EXPLODE(' ', $numbers);
   $d = COUNT($a);
   FOR ($i=$d;$i;$i--) { $c = $c + $a[$i]; }
   RETURN NUMBER_FORMAT($c/$d,0);
}
 
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.