array_sum()

array_sum() — Calculate the sum of values in an array.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

array_sum(array $array): int|float

Parameters

ParameterTypeWhat it takes
$arrayarrayThe input array.

Return value

Returns the sum of values as an integer or float; 0 if the array is empty.

Example

A runnable snippet:

$a = array(2, 4, 6, 8); echo "sum(a) = " . array_sum($a) . "\n"; $b = array("a" => 1.2, "b" => 2.3, "c" => 3.4); echo "sum(b) = " . array_sum($b) . "\n";

Output:

sum(a) = 20 sum(b) = 6.9

Changelog

VersionChange
8.3.0Now emits E_WARNING when array values cannot be converted to int or float. Previously arrays and objects where ignored whilst every other value was cast to int. Moreover, objects that define a numeric cast (e.g. GMP) are now cast instead of ignored.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page array_sum() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.