PHP range()Functions

PHP Array ReferenceComplete PHP Array Reference Manual

Examples

Create an array containing elements from "0" to "5":

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

Run example »

Definition and Usage

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

This function returns an array containing elements from low to high.

Note:If the low parameter is greater than the high parameter, the created array will be from high to low.


Syntax

range(low,high,step)

Parameters Description
low Required. Specifies the minimum value of the array elements.
high Required. Specifies the maximum value of the array elements.
step Optional. Specifies the step size between elements. Default is 1.

Technical Details

Return value: Returns an array containing elements fromlowtohighthe array of elements.
PHP Version: 4+
Changelog: stepThe step parameter was added in PHP 5.0.

In PHP 4.1.0 to 4.3.2 versions, the function treated numeric strings as strings rather than integers. Numeric strings were used for character sequences; for example, "5252" was treated as "5".

Support for character sequences and decreasing arrays was added in PHP 4.1.0. Character sequence values are limited to a length of one. If the length is greater than one, only the first character is used. Before this version, range() only generated increasing integer arrays.


More Examples

Example 1

Return an array containing elements from "0" to "50" in increments of 10:

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

Run example »

Example 2

Using letters - returns an array containing elements from "a" to "d":

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

Run example »


PHP Array ReferenceComplete PHP Array Reference Manual Other Extensions