w3resource

PHP : array_sum() function

PHP: Calculate the sum of values in an array

The array_sum() function is used to calculate the sum of values in the array.

Version:

(PHP 4 and above)

Syntax:

array_sum(array_name)

Parameter:

Name Description Required /
Optional
Type
array_name Specifies the name of the array. Required Array

Return value:

The sum of values.

Value Type: Integer or float.

Example:

<?php
$val1 = array(10, 5, 100);
$val2 = array(10.5, -5.5, 100);
echo "sum(val1) = ". array_sum($val1) . "<br />";
echo "sum(val2) = ". array_sum($val2) . "<br />";
?>

Output:

sum(val1) = 115
sum(val2) = 105

Pictorial Presentation:

php function reference: array_sum() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: array_slice
Next: array_unique



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php/function-reference/array_sum.php