range

(PHP 4, PHP 5, PHP 7, PHP 8)

rangeCreate an array containing a range of elements

Description

function range(string|int|float $start, string|int|float $end, int|float $step = 1): array

Create an array containing a range of elements.

If both start and end are strings, and step is int the produced array will be a sequence of bytes. Otherwise, the produced array will be a sequence of numbers.

The sequence is increasing if start is less than equal to end. Otherwise, the sequence is decreasing.

Parameters

start

First value of the sequence.

end

Last possible value of the sequence.

step

step indicates by how much is the produced sequence progressed between values of the sequence.

step may be negative for decreasing sequences.

If step is a float without a fractional part, it is interpreted as int.

Return Values

Returns a sequence of elements as an array with the first element being start going up to end, with each value of the sequence being step values apart.

The last element of the returned array is either end or the previous element of the sequence, depending on the value of step.

If both start and end are strings, and step is int the produced array will be a sequence of bytes, generally latin ASCII characters.

If at least one of start, end, or step is float the produced array will be a sequence of float.

Otherwise, the produced array will be a sequence of int.

Errors/Exceptions

  • If step is 0, a ValueError is thrown.

  • If start, end, or step is not is_finite(), a ValueError is thrown.

  • If step is negative, but the produced range is increasing (i.e. $start <= $end), a ValueError is thrown.

  • If start or end is the empty string '', an E_WARNING is emitted and the empty string will be interpreted as 0.

  • If start or end is a non-numeric string with more than one byte, an E_WARNING is emitted.

  • If start or end is a string that is implicitly cast to an int because the other boundary value is a number, an E_WARNING is emitted.

  • If step is a float, and start and end are non-numeric string, an E_WARNING is emitted.

Changelog

Version Description
8.3.0 If both start and end are strings then range() will now always produce an array of bytes. Previously if one of the boundary values was a numeric string, then the other boundary value was implicitly cast to int.
8.3.0 An E_WARNING is now emitted if start or end is a string that is implicitly cast to int because the other boundary value is a number.
8.3.0 An E_WARNING is now emitted if start or end is a non-numeric string with more than one byte.
8.3.0 An E_WARNING is now emitted if start or end is the empty string.
8.3.0 If step is a float with no fractional part, it will be interpreted as an int.
8.3.0 A ValueError is now thrown if step is negative when producing an increasing range.
8.3.0 A ValueError is now thrown if step is not finite.
8.3.0 A TypeError is now thrown if start or end is an array, object, or resource. Previously they were implicitly cast to int.

Examples

Example #1 range() examples

<?php
echo implode(', ', range(0, 12)), PHP_EOL;

echo implode(', ', range(0, 100, 10)), PHP_EOL;

echo implode(', ', range('a', 'i')), PHP_EOL;

echo implode(', ', range('c', 'a')), PHP_EOL;

echo implode(', ', range('A', 'z')), PHP_EOL;
?>

The above example will output:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
a, b, c, d, e, f, g, h, i
c, b, a
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, [, \, ], ^, _, `, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z

See Also

add a note

User Contributed Notes 30 notes

up
139
Palz
13 years ago
To create a range array like

Array
(
    [11] => 1
    [12] => 2
    [13] => 3
    [14] => 4
)

combine two range arrays using array_combine:

array_combine(range(11,14),range(1,4))
up
18
gtisza at gmail dot com
13 years ago
You might expect range($n, $n-1) to be an empty array (as in e.g. Python) but actually PHP will assume a step of -1 if start is larger than end.
up
14
php at keith tyler dot com
12 years ago
So with the introduction of single-character ranges to the range() function, the internal function tries to be "smart", and (I am inferring from behavior here) apparently checks the type of the incoming values. If one is numeric, including numeric string, then the other is treated as numeric; if it is a non-numeric string, it is treated as zero. 

But.

If you pass in a numeric string in such a way that is is forced to be recognized as type string and not type numeric, range() will function quite differently.

Compare:

<?php
echo implode("",range(9,"Q"));
// prints 9876543210

echo implode("",range("9 ","Q"));  //space after the 9
// prints 9:;<=>?@ABCDEFGHIJKLMNOPQ

echo implode("",range("q","9 "));
// prints qponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:987654
?>

I wouldn't call this a bug, because IMO it is even more useful than the stock usage of the function.
up
10
Alien426
10 years ago
The function will generate an array of integers even if your numerical parameters are enclosed in quotes.
<?php
var_dump