BigToach.com

PHP Snippets

avg

Information

(PHP Version >= 4.0.4)
Functions used in this snippet: is_array, sizeof, array_sum.

Description

int avg( array array )

Takes the array array and gives you the average of its values.

Snippet

<?php
function avg($array){
    if(
is_array($array) && sizeof($array) !== 0){
        return (
array_sum($array) / sizeof($array));
    }
    return 
false;
}
?>

Example

avg Can be used in the following way:

<?php
$array 
= array(1,3,4,9,41,5,87,6,62,4,8,6,8,7,5,6);

echo 
avg($array); // 16.375
?>

Added on Aug 8th at 5 am by Scott - 0 Comments


0 Responses to avg

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: