CKTIS / PHP
range()
Crea un array con una sequenza crescente o decrescente.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
range(string|int|float $start, string|int|float $end, int|float $step = 1): arrayCrea un array con una sequenza crescente o decrescente. L’estremo finale compare solo se il passo lo raggiunge esattamente. Lettere da un byte producono una sequenza di caratteri. Il passo zero genera ValueError e un intervallo grande occupa memoria per ogni elemento. Da PHP 8.3 due estremi testuali di un byte, anche cifre, producono stringhe. Un passo negativo in un intervallo crescente genera ValueError. Stringhe non numeriche più lunghe generano E_WARNING e contano solo i primi byte.
Firma
range(string|int|float $start, string|int|float $end, int|float $step = 1): array
Output dell’esempio
[[5,3,1],["a","c","e"]]
ValueError
[["3","4","5"],["a"],[true,true],"ValueError"]
Esempi
<?php
echo json_encode([range(5,0,2),range("a","e",2)]), PHP_EOL;
try { range(1,2,0); } catch (ValueError $e) { echo "ValueError"; }
$warnings=[]; set_error_handler(function($n) use (&$warnings){$warnings[]=$n===E_WARNING;return true;});
$bytes=range("ab","ac"); restore_error_handler();
try{range(1,5,-2);}catch(ValueError $e){$negative=get_class($e);}
echo PHP_EOL,json_encode([range("3","5"),$bytes,$warnings,$negative]);
Risultato
[[5,3,1],["a","c","e"]] ValueError [["3","4","5"],["a"],[true,true],"ValueError"]