PHP Manual
Prev |  | Next

# range

(PHP 3>= 3.0.8, PHP 4 )

range -- Create an array containing a range of elements

## Description

array range ( mixed low, mixed high [, int step])

range() returns an array of elements from `low` to `high`, inclusive. If low > high, the sequence will be from high to low.

> New parameter: The optional `step` parameter was added in 5.0.0.

If a `step` value is given, it will be used as the increment between elements in the sequence. `step` should be given as a positive number. If not specified, `step` will default to 1.

Example 1. range() examples
    <?php
    // array(0,1,2,3,4,5,6,7,8,9)
    foreach(range(0, 9) as $number) {
        echo $number;
    }

    // The step parameter was introduced in 5.0.0
    // array(0,10,20,30,40,50,60,70,80,90,100)
    foreach(range(0, 100, 10) as $number) {
        echo $number;
    }

    // Use of characters introduced in 4.1.0
    // array('a','b','c','d','e','f','g','h','i');
    foreach(range('a', 'i') as $letter) {
        echo $letter;
    }
    // array('c','b','a');
    foreach(range('c', 'a') as $letter) {
        echo $letter;
    }
    ?>

> Note: Prior to version 4.1.0 the range() function only generated incrementing integer arrays. Support for character sequences and decrementing arrays was added in 4.1.0.

See also shuffle() and foreach.

Prev | Home | Next
prev | Up | reset