title tags
average
math,beginner

Returns the average of two or more numbers.

  • Use array_sum() for all the values in $items and return the result divided by their count().
function average(...$items)
{
  $count = count($items);
  
  return $count === 0 ? 0 : array_sum($items) / $count;
}
average(1, 2, 3); // 2

Tags: PHP Average(), PHP array, two or more numbers,  count,  iterate array, search array,