range
Description
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 )
Links
Array
- array
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_fill
- array_fill_keys
- array_filter
- array_flip
- array_intersect
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge
- array_merge_recursive
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace
- array_replace_recursive
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff
- array_udiff_assoc
- array_udiff_uassoc
- array_uintersect
- array_uintersect_assoc
- array_uintersect_uassoc
- array_unique
- array_unshift
- array_values
- array_walk
- array_walk_recursive
- arsort
- asort
- compact
- count
- current
- end
- extract
- in_array
- key
- key_exists
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort