Core\Arr::sum
Core\Arr::sum(array<int|float|decimal> $a): int|float|decimalCore\Arr::sum( array<int|float|decimal> $a, ): int|float|decimal
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 runningintordecimaltotal goes outside its type's range. It is never converted tofloat. The error is also thrown when one array has both afloatand adecimal, 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";61480Prices 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";25.100.3 no0.3 yesThe 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";total: 4097 cents