Skip to content

Instantly share code, notes, and snippets.

@k-gun

k-gun/avg.php

Last active Dec 19, 2015
Embed
What would you like to do?
<?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
You can’t perform that action at this time.