PHP Array array_sum() Function (With Examples)

In this tutorial, we will learn about the PHP array_sum() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_sum() Function

The array_sum() is a built-in function of PHP, it returns sum of array elements.


The syntax of the array_sum() function:



The parameters of the array_sum() function:

  • $array_variable: It takes array as an argument and returns sum of all elements

Return Value

The return type of this method is int|float, it returns the sum of values as an integer or float; 0 if the array is empty. [Source]

Sample Input/Output

Array elements: {10, 20, 30, 40, 50}

Sum of all elements: 150

PHP array_sum() Function Example

//one dimensional array 
$arr = array(10, 20, 30, 40, 50);

//sum of the array elements
$sum = array_sum($arr);
echo ("Sum of array elements: " . $sum);


The output of the above example is:

Sum of array elements: 150

To understand the above examples, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.