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.

Syntax

The syntax of the array_sum() function:

array_sum($array_variable);

Parameters

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

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

Output:
Sum of all elements: 150

PHP array_sum() Function Example

<?php
//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);
?>

Output

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 www.includehelp.com. All rights reserved.