PHP 8 reference · Array Functions
PHP range() Function
Create an array of numbers or letters from a start to an end value, with an optional step, such as range(1, 10, 2).
PHP 8 Since 4.0
Syntax
range(string|int|float $start, string|int|float $end, int|float $step = 1): array
Parameters
| Parameter | Description |
|---|---|
start |
The first value. |
end |
The last value (included if the step lands on it). |
step optional |
The increment; must not be 0. Default: 1 |
Return value
An array of values.
Examples
<?php
echo implode(",", range(1, 5)), "\n";
echo implode(",", range(0, 20, 5)), "\n";
echo implode(",", range(5, 1)), "\n";
Output
1,2,3,4,5 0,5,10,15,20 5,4,3,2,1
<?php
echo implode("", range("a", "f")), "\n";
echo implode(",", range(0, 1, 0.25)), "\n";
Output
abcdef 0,0.25,0.5,0.75,1
Version history
In other languages
range()· Python
Related
array_fill()· Create an array filled with the same value, given a start index and a count.array_sum()· Return the sum of the values in an array. Numeric strings are converted; an empty array gives 0.- PHP Arrays · tutorial
- PHP Array Functions · tutorial