The range Function
The range function creates an array with a range of
elements. For example, you can create an array
filled with numbers from 1 to 100
or letters from 'a' to 'z'.
The range that the function generates is set by
parameters: the first parameter is the start,
and the second is the end.
The third optional parameter of the function sets the step with which the generation will proceed.
Syntax
range(string|int|float $start, string|int|float $end, int|float $step = 1): array
Example
Let's create an array filled with numbers
from 1 to 5:
<?php
var_dump(range(1, 5));
?>
Code execution result:
[1, 2, 3, 4, 5]
Example
Let's create an array filled with numbers
from 5 to 1:
<?php
var_dump(range(5, 1));
?>
Code execution result:
[5, 4, 3, 2, 1]
Example
Let's create an array filled with numbers
from 0 to 10 with a step of 2:
<?php
var_dump(range(0, 10, 2));
?>
Code execution result:
[0, 2, 4, 6, 8, 10]
Example
Let's create an array filled with letters
from 'a' to 'e':
<?php
var_dump(range('a', 'e'));
?>
Code execution result:
['a', 'b', 'c', 'd', 'e']
See Also
-
the
array_fillfunction,
which fills an array with a given value -
the
array_padfunction,
which pads an array to the specified length