PHP array_sum() Function

Example

Returns the sum of all values in the array (5+15+25):

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

Run Instance

Definition and Usage

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

If all values are integers, it returns an integer value. If one or more values are floating-point numbers, it returns a floating-point number.

Syntax

array_sum(array)
Parameter Description
array Required. Specify an array.

Technical Details

Return Value: Returns the sum of all values in the array.
PHP Version: 4.0.4+
Update Log: Before PHP 4.2.1 versions, the function modified the array passed in itself, converting the string values to numeric values (most of the time they were converted to zero, depending on the specific value).

More Examples

Example 1

Returns the sum of all values in the array (52.2+13.7+0.9):

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

Run Instance