PHP 8 reference · Array Functions

PHP array_sum() Function

Return the sum of the values in an array. Numeric strings are converted; an empty array gives 0.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_sum(array $array): int|float

Parameters

ParameterDescription
array The input array.

Return value

The sum as an int or float.

Examples

Add up valuesTry it yourself
<?php
echo array_sum([1, 2, 3.5]), "\n";
echo array_sum([]), "\n";

Output

6.5
0
Sum a columnTry it yourself
<?php
$cart = [["price" => 3.5], ["price" => 12]];
echo array_sum(array_column($cart, "price")), "\n";

Output

15.5

Version history

VersionChange
PHP 4.0Added
PHP 8.3Warns about values that can't be added (such as arrays) instead of silently skipping them.

In other languages

Was this page helpful?