PHP | array_sum() Function
The array_sum() function returns the sum of all the values in an array(one dimensional and associative). It takes an array parameter and returns the sum of all the values in it.
number array_sum ( $array )
Argument
The only argument to the function is the array whose sum needs to be calculated.
Return value
This function returns the sum obtained after adding all the elements together. The returned sum may be integer or float. It also returns 0 if the array is empty.
Examples:
Input : $a = array(12, 24, 36, 48);
print_r(array_sum($a));
Output :120
Input : $a = array();
print_r(array_sum($a));
Output :0
In the first example the array calculates the sum of the elements of the array and returns it. In the second example the answer returned is 0 since the array is empty.
Program – 1
<?php //array whose sum is to be calculated $a = array(12, 24, 36, 48); //calculating sum print_r(array_sum($a)); ?> |
Output:
120
Program – 2
<?php //array whose sum is to be calculated $a = array(); //calculating sum print_r(array_sum($a)); ?> |
Output:
0
Program – 3
<?php // array whose sum is to be calculated $b = array("anti" => 1.42, "biotic" => 12.3, "charisma" => 73.4); // calculating sum print_r(array_sum($b)); ?> |
Output:
87.12
Thanks to HGaur for providing above examples.
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.map.has() Function
- D3.js | d3.map.get() Function
- p5.js | value() Function
- p5.js | red() function
- p5.js | max() function
- p5.js | min() function
- p5.js | hue() function
- CSS | rgb() Function
- PHP | Ds\Set contains() Function
- PHP | Ds\Map last() Function
- D3.js | d3.set.add() Function
- D3.js | d3.hcl() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


