Quick Search:
 
 PHP Code: Simple Averaging Jump to:  
Category: >> PHP Code >> Simple Averaging  

<< lastnext >>

Snippet Name: Simple Averaging

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

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

<?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);
}
 
?>


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org