PHP array sum

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

Pictorial Presentation :

php array array_sum() function

Example :

  1. $val1 = array(10, 5, 100);  

  2. $val2 = array(10.5, -5.5, 100);  

  3. echo "sum(val1) = ". array_sum($val1) . "
    ";  

  4. echo "sum(val2) = ". array_sum($val2) . "
    ";  

  5. ?>  

Output :

sum(val1) = 115

sum(val2) = 105