range

Create an array containing a range of elements

Syntax

range ( mixed $start , mixed $end [, number $step = 1 ] ) : array

Parameters

start

First value of the sequence.

end

The sequence is ended upon reaching the end value.

step

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.

Return

Returns an array of elements from start to end, inclusive.

Examples

1 · start end · Numeric · Ascend

<?

$start = 0;
$end = 9;

$return = range($start, $end);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
)

2 · start end · Numeric · Descend

<?

$start = 9;
$end = 0;

$return = range($start, $end);

print_r($return);

?>
Array
(
    [0] => 9
    [1] => 8
    [2] => 7
    [3] => 6
    [4] => 5
    [5] => 4
    [6] => 3
    [7] => 2
    [8] => 1
    [9] => 0
)

3 · start end · Alphabetic · Ascend

<?

$start = "a";
$end = "j";

$return = range($start, $end);

print_r($return);

?>
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
)

4 · start end · Alphabetic · Descend

<?

$start = "j";
$end = "a";

$return = range($start, $end);

print_r($return);

?>
Array
(
    [0] => j
    [1] => i
    [2] => h
    [3] => g
    [4] => f
    [5] => e
    [6] => d
    [7] => c
    [8] => b
    [9] => a
)

5 · step

<?

$start = 0;
$end = 9;
$step = 3;

$return = range($start, $end, $step);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 3
    [2] => 6
    [3] => 9
)
HomeMenu