1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Improved; |
||
6 | |||
7 | /** |
||
8 | * Calculate the sum of all numbers. |
||
9 | * If no elements are present, the result is 0. |
||
10 | * |
||
11 | * @param iterable $iterable |
||
12 | * @return int|float |
||
13 | */ |
||
14 | function iterable_sum(iterable $iterable) |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
15 | { |
||
16 | 13 | if (is_array($iterable)) { |
|
17 | 2 | return array_sum($iterable); |
|
18 | } |
||
19 | |||
20 | 11 | $sum = 0; |
|
21 | |||
22 | 11 | foreach ($iterable as $item) { |
|
23 | 10 | $sum += $item; |
|
24 | } |
||
25 | |||
26 | 11 | return $sum; |
|
27 | } |
||
28 |