Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

range

Description

The range of Array for PHP create an array containing a range of elements.

Syntax

range(
    string|int|float $start,
    string|int|float $end,
    int|float $step = 1
): array

Parameters

start

First value of the sequence.

end

Last possible value of the sequence.

step

step indicates by how much is the produced sequence progressed between values of the sequence.

step may be negative for decreasing sequences.

If step is a float without a fractional part, it is interpreted as int.

Return

Returns a sequence of elements as an array with the first element being start going up to end, with each value of the sequence being step values apart.

The last element of the returned array is either end or the previous element of the sequence, depending on the value of step.

If both start and end are strings, and step is int the produced array will be a sequence of bytes, generally latin ASCII characters.

If at least one of start, end, or step is float the produced array will be a sequence of float.

Otherwise, the produced array will be a sequence of int.

Examples

1 · start end · string · ascend

<?

$start = "a";
$end = "z";

$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
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [16] => q
    [17] => r
    [18] => s
    [19] => t
    [20] => u
    [21] => v
    [22] => w
    [23] => x
    [24] => y
    [25] => z
)

2 · start end · string · descend

<?

$start = "z";
$end = "a";

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

print_r($return);
Array
(
    [0] => z
    [1] => y
    [2] => x
    [3] => w
    [4] => v
    [5] => u
    [6] => t
    [7] => s
    [8] => r
    [9] => q
    [10] => p
    [11] => o
    [12] => n
    [13] => m
    [14] => l
    [15] => k
    [16] => j
    [17] => i
    [18] => h
    [19] => g
    [20] => f
    [21] => e
    [22] => d
    [23] => c
    [24] => b
    [25] => a
)

3 · start end · int · 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
)

4 · start end · int · 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
)

5 · start end · float · ascend

<?

$start = 0.5;
$end = 9.5;

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

print_r($return);
Array
(
    [0] => 0.5
    [1] => 1.5
    [2] => 2.5
    [3] => 3.5
    [4] => 4.5
    [5] => 5.5
    [6] => 6.5
    [7] => 7.5
    [8] => 8.5
    [9] => 9.5
)

6 · start end · float · descend

<?

$start = 9.5;
$end = 0.5;

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

print_r($return);
Array
(
    [0] => 9.5
    [1] => 8.5
    [2] => 7.5
    [3] => 6.5
    [4] => 5.5
    [5] => 4.5
    [6] => 3.5
    [7] => 2.5
    [8] => 1.5
    [9] => 0.5
)

7 · step · int

<?

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

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

print_r($return);
Array
(
    [0] => 0
    [1] => 3
    [2] => 6
    [3] => 9
)

8 · step · float

<?

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

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

print_r($return);
Array
(
    [0] => 0
    [1] => 4.5
    [2] => 9
)
HomeMenu