PHP » Arrays » range()

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

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.

Examples

Code:
<?php

// 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 ";
}

?>
Output:
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
Explanation:

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

See Also: