PHP range() function

Example

Create an array containing a range of elements from "0" to "5":

<?php
$number = range(0,5);
print_r($number);
?>

Run Instances

Definition and Usage

The range() function creates an array containing elements of a specified range.

The function returns an array containing elements from low to high between

Note:If low is greater than high Parameters high to low.

Description

This function creates an array containing elements from low to high (including low and highbetween the integers or characters. high is low is smaller, the array returned will be in reverse order.

Syntax

range(low,high,step)
Parameters Description
low Required. Specifies the minimum value of the array.
high Required. Specifies the maximum value of the array.
step Optional. Specifies the step between elements. The default is 1.

Technical Details

Return Value: Return an array containing elements from low to high elements array.
PHP Version: 4+
Update Log:

The step parameter was added in PHP 5.0.

In versions from PHP 4.1.0 to 4.3.2, the function treats numeric strings as strings rather than integers. Numeric strings will be used for character sequences, for example, "5252" is considered as "5".

Support for character sequences and descending arrays was added in PHP 4.1.0. The value of the character sequence is limited to a length. If the length is greater than one, only the first character is used. In versions prior to this, range() only generated ascending integer arrays.

More Examples

Example 1

Return an array containing elements from "0" to "50" with an increment of 10:

<?php
$number = range(0,50,10);
print_r($number);
?>

Run Instances

Example 2

Use letters - return an array containing elements from "a" to "d":

<?php
$letter = range("a","d");
print_r($letter);
?>

Run Instances