Skip to content

Instantly share code, notes, and snippets.

@krmgns
Last active December 19, 2015 09:58
Show Gist options
  • Save krmgns/5936481 to your computer and use it in GitHub Desktop.
Save krmgns/5936481 to your computer and use it in GitHub Desktop.
<?php
function avg(){
return array_sum(func_get_args()) / func_num_args();
}
function avgr($a){
return array_sum($a) / count($a);
}
function avg() {
$sum = 0;
$num = func_num_args();
for($i = 0; $i < $num; $i++) {
$sum += func_get_arg($i);
}
$avg = $sum / $num;
return $avg;
}
function avgr($a) {
$sum = 0;
$num = count($a);
for($i = 0; $i < $num; $i++) {
$sum += $a[$i];
}
$avg = $sum / $num;
return $avg;
}
printf("%.2f\n", avg(2,1,2,1,3,4,5,1,3,6));
printf("%.2f\n", avgr(array(2,1,2,1,3,4,5,1,3,6)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment