The array_sum Function

The array_sum function calculates the sum of array elements.

Syntax

phparray_sum(array $array): int|float

Example

Let's find the sum of array elements:

php<?php
  $arr = [1, 2, 3, 4, 5];
  echo array_sum($arr);
?>

Code execution result:

php15

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<?php
  $num = 12345;
  echo array_sum(str_split($num, 1));
?>

Code execution result:

php15

See Also

  • the array_product function,
    which finds the product of array elements