Home »
PHP
array_sum() function in PHP
In this article, we are going to learn how to find sum of all array elements using array_sum() function in PHP?
Submitted by IncludeHelp, on January 03, 2018
Given a one dimensional array and we have to find sum of all elements using PHP.
array_sum() function
It is a built-in function of PHP, it returns sum of array elements.
Syntax:
array_sum($array_variable);
It takes array as an argument and returns sum of all elements.
Example:
Input:
Array elements: {10, 20, 30, 40, 50}
Output:
Sum of all elements: 150
Program:
<?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
Sum of array elements: 150
TOP Interview Coding Problems/Challenges