Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

range()

Example

Generate an array with elements ranging from “0” to “5”.

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

Definition and Usage

The range() function generates an array of elements within a specified range.

This function returns an array starting from the low value up to the high value.

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

Syntax

range(low, high, step)

Parameter Values

 

Parameter

Description

low

Required. Specifies the minimum value for the array.

high

Required. Specifies the maximum value for the array.

step

Optional. Specifies the increment for the range. The default value is 1.

Technical Details

Return Value:

Returns an array with elements ranging from the low value to the high value.

PHP Version:

4+

PHP Changelog:

The step parameter was introduced in PHP 5.0.

In PHP versions 4.1.0 through 4.3.2, this function treats numeric strings as strings rather than integers. For example, “5252” is processed as “5” when generating character sequences.

Support for character sequences and decrementing arrays was added in PHP 4.1.0. Character sequences are limited to a single character; if the length exceeds one, only the first character is used. Prior to this version, range() only generated arrays with incrementing integers.

More Examples

Example

Return an array of elements ranging from “0” to “50”, with an increment of 10.

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

Example

Using letters, return an array of elements ranging from “a” to “d”.

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