In this tutorial, you shall learn about PHP array_sum() function which can find the sum of all values in the array, with syntax and examples.

PHP array_sum() Function

The PHP array_sum() function computes sum (addition) of all numbers in an array and returns the result.

If array contains items of different datatypes, only the numbers are considered for addition operation.

Syntax of array_sum()

The syntax of array_sum() function is

</>
Copy
array_sum( array )

where

ParameterDescription
array[mandatory] The array whose elements’ sum has to be computed.

Function Return Value

array_sum() returns the sum of all numbers in an array.

Examples

1. Find the sum of numbers in an Array

In this example, we will take an array with three numbers. We will pass this array as argument to array_sum() function. The function computes the sum of numbers in the array and returns the result.

PHP Program

</>
Copy
<?php
$array1 = array(5, 7, 2);
$result = array_sum($array1);
print_r($array1);
echo "<br>Sum is: ";
echo $result;
?>

Output