range
Creates an array holding a sequence of values.
Syntax
range(string|int|float $start, string|int|float $end, int|float $step = 1): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| $start | string|int|float | The first value. |
| $end | string|int|float | The last value, inclusive. |
| $step | int|float | Optional. The step; 1 by default. |
Return value
arrayAn array of the values from start to end.
Examples
Numbers, and numbers with a step:
<?php
echo implode(', ', range(1, 5)), "\n";
echo implode(', ', range(0, 100, 25));Output:
1, 2, 3, 4, 5
0, 25, 50, 75, 100Reverse order, and letters:
<?php
echo implode(', ', range(5, 1)), "\n";
echo implode('', range('a', 'e'));Output:
5, 4, 3, 2, 1
abcdeNotes
Both bounds are included. For very long sequences a generator is more economical.
See also
- array_fill — Creates an array of identical values.
- array_map — Applies a function to every element of an array.
Related recipes
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.