PHP Tutorial - PHP array_sum() Function






Definition

The array_sum() function returns the sum of all the values in the array.

Syntax

PHP array_sum() Function has the following syntax.

array_sum(array)

Parameter

  • array - Required. Array to sum

Example 1

Sum an indexed array


<?php
    $a=array(5,15,25);
    echo array_sum($a);
?>

The code above generates the following result.





Example 2

Return the sum of all the values in the array (2.2+1.7+0.9):


<?php
   $a=array("a"=>2.2,"b"=>1.7,"c"=>0.9);
   echo array_sum($a);
?>

The code above generates the following result.