CKTIS / PHP
Tip PHP iterable
Sprejme polje ali objekt, ki izvaja Traversable.
Različica: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
function total(iterable $values): intSprejme polje ali objekt, ki izvaja Traversable. foreach obhodi obe obliki brez predpostavke o velikosti ali dostopu z indeksom. Generator se lahko izčrpa. Za drugi obhod ustvarite novega ali shranite vrednosti. Niz ne ustreza temu tipu.
declare(strict_types=1);
function total(iterable $values): int { $n = 0; foreach ($values as $v) { $n += $v; } return $n; }
function numbers(): Generator { yield 2; yield 5; }
echo json_encode([total([2,5]), total(numbers()), is_iterable('25')]);
[7,7,false]
Primeri
<?php
declare(strict_types=1);
function total(iterable $values): int { $n = 0; foreach ($values as $v) { $n += $v; } return $n; }
function numbers(): Generator { yield 2; yield 5; }
echo json_encode([total([2,5]), total(numbers()), is_iterable('25')]);
Rezultat
[7,7,false]