Backend Development 5 min read

Using PHP’s array_sum() Function to Sum Array Elements

This article explains PHP’s built‑in array_sum() function, detailing its syntax, parameters, return values, and behavior with numeric and non‑numeric elements, and provides clear code examples demonstrating how to sum array values and handle edge cases such as empty arrays.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP’s array_sum() Function to Sum Array Elements

In PHP, arrays are a commonly used data structure for storing related data, and developers often need to calculate the sum of all elements in an array. PHP provides a built‑in function called array_sum() to simplify this task.

The array_sum() function computes the sum of all elements in an array and returns the result. It works with any numeric array, whether the elements are integers or floating‑point numbers. Non‑numeric elements in the array are ignored.

Below we detail the usage of array_sum() and provide several example code snippets.

Syntax:

array_sum(array $array): number

Parameter Description:

$array : The array whose elements are to be summed. It must be a numeric array.

Return Value:

The function returns the sum of all elements in the array. If the array is empty, it returns 0 .

Example Code:

<?php
// Example array
$numbers = [1, 2, 3, 4, 5];
// Calculate the sum of all elements
$sum = array_sum($numbers);
// Output the result
echo "Array sum is: $sum";
?>

In this example, we define an array $numbers containing a series of numbers, call array_sum() with that array, and assign the returned sum to $sum . The result is then printed using echo , producing "Array sum is: 15".

If the array is empty, array_sum() returns 0 because there are no elements to contribute to the sum.

When the array passed to array_sum() contains non‑numeric elements, those elements are ignored. Strings, booleans, or other non‑numeric types do not affect the calculation.

Non‑Numeric Elements Example:

<?php
// Example array with mixed types
$numbers = [1, 2, "3", 4, true];
// Calculate the sum
$sum = array_sum($numbers);
// Output the result
echo "Array sum is: $sum";
?>

In this case, the array $numbers includes a string "3" and a boolean true . Both are ignored by array_sum() , so the function returns 7 , demonstrating that non‑numeric values do not influence the sum.

In summary, the array_sum() function is a practical tool for quickly summing numeric arrays in PHP. It handles both integer and floating‑point numbers, returns 0 for empty arrays, and safely ignores non‑numeric elements.

Backend DevelopmentCode Examplearray_sumnumeric array
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.