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:

View the example in the browser
Practice here online :
See also
Previous: array_slice
Next: array_unique
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises