Home >>PHP Array Functions >PHP array_sum() Function
PHP array_sum() function is used to calculate the sum of all the values in any given array. It accepts only a single parameter $array whose sum needs to be calculated. Itreturns the sum obtained after adding all the elements of the given array together.
Syntax:
array_sum($array);
Parameter | Description |
---|---|
array | This is a required parameter. This parameter defines the given array. |
Here is an example of array_sum() function in PHP:
<html> <body> <?php $x=array(1,2,3,4,5,6,7,8,9); echo array_sum($x); ?> </body> </html>
Example 2:
<html> <body> <?php for($i=0;$i<=1000;$i++) { $x[]=$i; } echo array_sum($x); ?> </body> </html