Skip to content

Core\Arr::sum

Adds up every value in an array and gives you the total.

The type of the total follows the values it meets. Whole numbers added to whole numbers stay whole. One float anywhere makes the total a float. One decimal anywhere makes the total a decimal, which is the type to use for money.

A total that grows past the range a whole number covers throws an error. It does not wrap around and does not turn into a float, so a total is always exact.

A float and a decimal in the same array throw an error as well. There is no type that holds both of them, so there is no total to give you.

An array with no entries gives you 0.

The examples show a list of whole numbers, prices added up as decimal, and the total of a shopping cart worked out from its lines.

Parameters

  • $aarray<int|float|decimal>The numbers to add.

Return value

Returns int|float|decimal. The total, of the widest type met; 0 as an int for the empty array.

Errors

  • RuntimeErrorThe running int or decimal total goes outside its type's range. It is never converted to float. The error is also thrown when one array has both a float and a decimal, because they have no common type.

Examples

Adding up a list of numbers

<?nvs
// Shows how to add up the values of an array.
array<int> $goals = [3, 0, 2, 1];
// This prints 6. The keys are not used, only the values.
echo Core\Arr::sum($goals), "\n";
// A map works the same way. This prints 148.
array<int> $stock = ['screws' => 120, 'nails' => 25, 'bolts' => 3];
echo Core\Arr::sum($stock), "\n";
// An array with no entries gives you 0, so a total is always a number you
// can print. This prints 0.
array<int> $nothing = [];
echo Core\Arr::sum($nothing), "\n";
Output
6
148
0

Prices added up exactly

<?nvs
// Shows how to add up money. A `decimal` holds the value you wrote, so the
// total is exact and no cent goes missing.
array<decimal> $prices = [19.99, 5.01, 0.10];
// One `decimal` in the array makes the total a `decimal`. This prints 25.10.
echo Core\Arr::sum($prices), "\n";
// A `float` cannot hold 0.1 exactly. This total prints as 0.3 and is not
// equal to 0.3, so the line reads "0.3 no".
array<float> $shares = [0.1, 0.2];
echo Core\Arr::sum($shares), ' ', Core\Arr::sum($shares) == 0.3 ? 'yes' : 'no', "\n";
// The same two values as `decimal`. This total is exact, so the line reads
// "0.3 yes".
array<decimal> $exact = [0.1, 0.2];
echo Core\Arr::sum($exact), ' ', Core\Arr::sum($exact) == (0.3 as decimal) ? 'yes' : 'no', "\n";
Output
25.10
0.3 no
0.3 yes

The total of a shopping cart

<?nvs
// Shows how to work out the total of a shopping cart. Every line has a price
// in cents and a quantity, and the lines are added up at the end.
array<array<int>> $cart = [
['price' => 1299, 'quantity' => 2],
['price' => 499, 'quantity' => 1],
['price' => 250, 'quantity' => 4],
];
// `Core\Arr::map` turns every line into what that line costs.
array<int> $lines = Core\Arr::map(
$cart,
fn(array<int> $line): int => $line['price'] * $line['quantity'],
);
// This prints 4097.
echo 'total: ', Core\Arr::sum($lines), " cents\n";
Output
total: 4097 cents