PHP average function to get an average

2.5K views
no comments
27 Jul 2016 3:00 am

Define get_avg function

This function can give result with an array or argument, If need this code copy and defined it in your project library but don’t republish it anywhere share and comment or improvements suggestion for better code will be appreciated.

function get_avg($arrayORArgs){
	if(is_array($arrayORArgs)){
		return array_sum($arrayORArgs) / count($arrayORArgs);
	} else if(is_numeric($arrayORArgs)){
		$func_info = debug_backtrace();
		return array_sum($func_info[0]['args']) / count($func_info[0]['args']);
	}
	return 0;
}

Call get_avg function with array argument.

The function return result 27.5 with array argument

$array_average = array(50,10,20,30);
echo get_avg($array_average);

Call get_avg function with number arguments.

The function return result 27.5 with number arguments

echo get_avg(50,10,20,30);

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>