Content · Programming · PHP

PHP array_sum() Function: Usage and Examples

Understand array_sum() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

2 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to calculate the total of numeric values in an array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The array_sum() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is array_sum() in PHP?

Calculate the total of numeric values in an array.

Its syntax is:

array_sum(array $array): int|float

Parameters used by this operation:

  • $array — The collection being examined or updated.

Inputs are interpreted numerically; avoid feeding user strings without validation. For money, decimal rounding and integer cents deserve special care.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$invoiceLines = [19.95, 10.00, 5.50];
echo array_sum($invoiceLines);

Output:

35.45

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$quantities = ["red" => 4, "blue" => 3, "green" => 2];
echo array_sum($quantities);

Output:

9

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Add prevalidated invoice line totals before displaying a subtotal

Add prevalidated invoice line totals before displaying a subtotal. Save the code in a PHP file and adapt the sample data to your project.

<?php

$orderQuantities = [3, 2, 4];
$totalItems = array_sum($orderQuantities);
echo "Items to pack: " . $totalItems;

Output:

Items to pack: 9

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

Inputs are interpreted numerically; avoid feeding user strings without validation. For money, decimal rounding and integer cents deserve special care.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
array_sum()Calculate the total of numeric values in an array
Related functionscount() counts entries rather than summing values; array_reduce() supports custom calculations.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming input and result have identical keysCheck whether the function reindexes keys or retains original associations.
Confusing mutation with a returned copyInputs are interpreted numerically; avoid feeding user strings without validation. For money, decimal rounding and integer cents deserve special care.
Choosing a related function with different behaviorcount() counts entries rather than summing values; array_reduce() supports custom calculations.

Key Takeaways

  • array_sum() helps you calculate the total of numeric values in an array.
  • Inputs are interpreted numerically; avoid feeding user strings without validation. For money, decimal rounding and integer cents deserve special care.
  • count() counts entries rather than summing values; array_reduce() supports custom calculations.
  • Use the practical example as a starting point, but validate real application inputs.

array_sum() is useful when you need to calculate the total of numeric values in an array. Use its documented rules and the examples above to choose the right behavior for your data.

For additional details, see the official PHP documentation.

Share this resourceSend this page to someone who may find it useful.