PHP » Arrays » range()

array range(int lower, int upper [, int step])
The lower limit of the range.
The upper limit of the range.

Automatically assigns values to an array based on a range.

The range() function offers a convenient way of filling an array with values based on a range with lower and upper limits specified. As of PHP 5.0.0, an optional stepping parameter is available. The stepping parameter controls how many values should be skipped between each element. The range is returned as an array.

In versions lower than 4.1.0, the ranges can only be numerical, but 4.1.0 and up support character ranges also.



// array with the values 0, 1, 2, ... 100
$array1 = range(0, 100);

// array with the values 10, 20, ... 90. PHP 5 required!
//$array2 = range(10, 90, 10);

// array with the letters of the alphabet, backwards
$array3 = range('z', 'a');

foreach ($array3 as $letter) {
   print "$letter ";

z y x w v u t s r q p o n m l k j i h g f e d c b a

The code shows a few examples of usage. The result of the third example is printed.

See Also: