Array Functions

PHP array_sum() Function

Topic: PHP Array ReferencePrev|Next


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

The following table summarizes the technical details of this function.

Return Value: Returns the sum of all the values in an array as an integer or float; 0 if the array is empty.
Version: PHP 4.0.4+


The basic syntax of the array_sum() function is given with:


The following example shows the array_sum() function in action.

// Sample array
$numbers = array(1, 2, 5, 7, 10);

// Getting the sum of array values
echo array_sum($numbers); // Prints: 25


The array_sum() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to work on.

More Examples

Here're some more examples showing how array_sum() function actually works:

You can also use this function with associative arrays having numeric values, as shown here:

// Sample array
$persons = array("Harry"=>18, "Clark"=>32, "John"=>24);

// Getting the sum of array values
echo array_sum($persons); // Prints: 74
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties