Computer >> Computer tutorials >  >> Programming >> PHP

array_sum() function in PHP


The array_sum() function returns the sum of the values in an array. The returned value can be integer or float. It returns 0 if the array is empty.

Syntax

array_sum(arr)

Parameters

  • arr − the specified array

Return

The array_sum() function returns the sum of values. The returned value can be integer or float. It returns 0 if the array is empty.

Example

The following is an example −

<?php
$arr = array(50, 100, 150, 300);
echo array_sum($arr);
?>

Output

The following is the output −

600

Example

Let us see another example −

<?php
$arr = array("a"=>29.8,"b"=>66.4);
echo array_sum($arr);
?>

Output

The following is the output −

96.2