The array_sum Function
The array_sum function calculates the sum
of array elements.
Syntax
array_sum(array $array): int|float
Example
Let's find the sum of array elements:
<?php
$arr = [1, 2, 3, 4, 5];
echo array_sum($arr);
?>
Code execution result:
15
Example . Application
Let's find the sum of digits of a number. To do this,
split the number into an array using str_split
and add the elements of this array using
array_sum:
<?php
$num = 12345;
echo array_sum(str_split($num, 1));
?>
Code execution result:
15
See Also
-
the
array_productfunction,
which finds the product of array elements