Blog

PHP Function to Echo Out the Average of an Array
Posted on June 16, 2015 in PHP by Matt Jennings

<?php
// PHP function to echo out the average of an array

function compute_average($arr_to_avg)
{
    $number_of_values = count($arr_to_avg);
    $total = 0;

    foreach($arr_to_avg as $value)
    {
        $total = $total + $value;
    }
    echo $total / $number_of_values;

}

$x = array(1, 5, 12);

compute_average($x);

?>

Leave a Reply