PHP array_sum() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_sum() adds up all the values in an array and returns the total.

If every value is a whole number the result is an integer. If any value is a float, the result is a float. An empty array gives 0.

Syntax

Call array_sum() with the values you need. Start with this line.

PHP
$total = array_sum([2, 4, 6]);
CallWhat it does
array_sum([2, 4, 6])Returns 12.
array_sum([1.5, 2.5])Returns 4 as a float.
array_sum([])Returns 0.

array_sum() is great for totals such as shopping carts and scores. Divide by count() to get an average.

Library declarations
PHP
array_sum(array $array): int|float

Parameters

ParameterDescription
arrayRequired. The array of values to add together.

Return Value

Return ValueDescription
int|floatThe sum of all values. Returns 0 for an empty array.

PHP Examples

Example 1 — Add Up Numbers

Sum a short list of whole numbers.

PHP
<?php
$numbers = [2, 4, 6];
$total = array_sum($numbers);

echo "Total: ", $total, PHP_EOL;

How It Works

  1. Create an array with the numbers 2, 4 and 6.
  2. array_sum() adds them: 2 + 4 + 6.
  3. The total, 12, is stored in $total.
  4. echo prints the result.

Example 2 — Numeric Strings and Empty Arrays

Numeric strings are converted to numbers, and an empty array sums to 0.

PHP
<?php
echo array_sum([1, "2", "3.5"]), PHP_EOL;
echo array_sum([]), PHP_EOL;

How It Works

  1. The strings "2" and "3. 5" look like numbers, so PHP converts them.
  2. 1 + 2 + 3. 5 equals 6.5, which is a float.
  3. An empty array has nothing to add.
  4. The result for an empty array is the integer 0.

Example 3 — Cart Total and Average

Combine array_sum() with count() and number_format().

PHP
<?php
$prices = [4.5, 2.25, 3.25];
$total = array_sum($prices);
$average = $total / count($prices);

echo "Total: ", number_format($total, 2), PHP_EOL;
echo "Average: ", number_format($average, 2), PHP_EOL;

How It Works

  1. Store three item prices in an array.
  2. array_sum() returns 10 as the cart total.
  3. Dividing by count() (which is 3) gives the average price.
  4. number_format() prints both values with two decimal places.

Frequently Asked Questions

It adds all the values in an array and returns the total as an int or float.
Divide array_sum($array) by count($array).
It returns 0.
Yes. Strings like "5" or "2.5" are converted to numbers before they are added.
Use array_column() to pull out that column first, then pass the result to array_sum().

Next: array_udiff()

array_udiff() returns the values from the first array that are not found in the other arra.

array_udiff() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful