PHP array_sum() function
Syntax
array_sum($array);
Parameter | Description |
Array | Any array variable (required) |
Definition
It returns sum of an array type an integer or float.
Usage
array_sum() function is used to calculate the sum of an array.
Example of Array sum function
<?php $num = array(20, 50, 10); echo "sum of array = " . array_sum($num); ?>
- Example
- Run
$num is an array variable that hold three integer. You can add all the values in an array, with the help of array_sum() function as shown in above.
More example of Array sum function
<?php $num = array(2.25, 5.65, 1.15); echo "sum of array = " . array_sum($num); ?>
- Example
- Run
You can calculate the sum of integer array as well as float array.
<?php $num = array(38, 7.65, 1.15, 'Johns',12); echo "sum of array = " . array_sum($num); ?>
sum of array = 58.8
In case of combine array that hold integer, float and string, array sum function add the integer, float and return the result.