Average calculator

Returns the average of the numbers given.

<?php

function average($numbers=array())
{
	if (!is_array($numbers))
		$numbers = func_get_args();
	
	$sum = 0;
	$amt = count($numbers);
	
	foreach($numbers as $num)
		$sum += $num;
			
	return ($amt > 0) ? ($sum / $amt) : false; // no division by zero
}


/*
echo average(array(3,4,5)); // 4
echo average(50,30,40); // 40
*/

?>

Usage

As seen in the commented portion of code, this function can take either an array of numbers, or multiple parameters of numbers.


Comments

guest
Posted on 21.01.2011 07:26

do it need textboxes to enter the numbers???? or else...

Add your comment