php8.5
Home/ Manual/ ds / set/ Ds\Set::sum

Ds\Set::sum

PHP function Edit on GitHub ✎

(PECL ds >= 1.0.0)

Returns the sum of all values in the set

Description

Ds\Set::sum(): int|float

Returns the sum of all values in the set.

Note

Arrays and objects are considered equal to zero when calculating the sum.

Parameters Parameters

Return Values

The sum of all the values in the set as either a float or int depending on the values in the set.

Examples

Ds\Set::sum() integer example

php
<?php
$set = new \Ds\Set([1, 2, 3]);
var_dump($set->sum());
?>

The above example will output something similar to:

output
int(6)

Ds\Set::sum() float example

php
<?php
$set = new \Ds\Set([1, 2.5, 3]);
var_dump($set->sum());
?>

The above example will output something similar to:

output
float(6.5)

Source: reference/ds/ds/set/sum.xml · from the official PHP manual (php/doc-en)