The range[] function is an inbuilt function in PHP which is used to create an array of elements of any kind such as integer, alphabets within a given range[from low to high] i.e, list’s first element is considered as low and last one is considered as high.
Syntax:
array range[low, high, step]
Parameters: This function accepts three parameters as described below:
- low: It will be the first value in the array generated by range[] function.
- high: It will be the last value in the array generated by range[] function.
- step: It is used when the increment used in the range and it’s default value is 1.
Return Value: It returns an array of elements from low to high.
Examples:
Input : range[0, 6] Output : 0, 1, 2, 3, 4, 5, 6 Explanation: Here range[] function print 0 to 6 because the parameter of range function is 0 as low and 6 as high. As the parameter step is not passed, values in the array are incremented by 1. Input : range[0, 100, 10] Output : 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 Explanation: Here range[] function accepts parameters as 0, 100, 10 which are values of low, high, step respectively so it returns an array with elements starting from 0 to 100 incremented by 10.
Below programs illustrate range[] function in PHP:
Program 1:
Output:
0 1 2 3 4 5 6
Program 2:
Output:
0 20 40 60 80 100
Program 3:
Output:
a b c d e f g h i j
Program 4:
Output:
p o n m l k j i h g f e d c b a
Reference:
//php.net/manual/en/function.range.php
[PHP 4, PHP 5, PHP 7, PHP 8]
range — Create an array containing a range of elements
Description
range[string|int|float $start
, string|int|float $end
,
int|float $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 [or decrement] between elements in the sequence. step
must not
equal 0
and must not exceed the specified range. If not specified, step
will default to 1.
Return Values
Returns an array of elements from start
to end
, inclusive.
Examples
Example #1 range[] examples
Notes
Note:
Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.
Palz ¶
9 years ago
To create a range array like
Array
[
[11] => 1
[12] => 2
[13] => 3
[14] => 4
]
combine two range arrays using array_combine:
array_combine[range[11,14],range[1,4]]
luca.favorido ATgmailDOT com ¶
6 years ago
The function "range" is very useful to get an array of characters as range['C','R'] does.
At work, I had to extend the function range[$a,$b] to work in this special case: with two uppercase strings $a and $b, it should return all the possible strings between $a and $b.
This could be used for example to get the excel column indexes.
e.g.
So I wrote the function getrange[$min,$max] that exactly does this.
php at keith tyler dot com ¶
8 years ago
So with the introduction of single-character ranges to the range[] function, the internal function tries to be "smart", and [I am inferring from behavior here] apparently checks the type of the incoming values. If one is numeric, including numeric string, then the other is treated as numeric; if it is a non-numeric string, it is treated as zero.
But.
If you pass in a numeric string in such a way that is is forced to be recognized as type string and not type numeric, range[] will function quite differently.
Compare:
=
I wouldn't call this a bug, because IMO it is even more useful than the stock usage of the function.
ThinkMedical at Gmail dot com ¶
14 years ago
foreach[range[]] whilst efficiant in other languages, such as python, it is not [compared to a for] in php*.
php is a C-inspired language and thus for is entirely in-keeping with the lanuage aethetic to use it
That the officiant documentation doesnt mention the for loop is strange.
Note however, that in PHP5 foreach is faster than for when iterating without incrementing a variable.
* My tests using microtime and 100 000 iterations consistently [~10 times] show that for is 4x faster than foreach[range[]].
ccb_bc at hotmail dot com ¶
3 years ago
ktamas77 at gmail dot com ¶
10 years ago
if you need zero padding, string prefixes or any other masks, then a simple combination of array_map, inline functions and sprintf is your friend.
Array
Will result:
[
[0] => sample_050
[1] => sample_051
[2] => sample_052
[3] => sample_053
[4] => sample_054
[5] => sample_055
[6] => sample_056
[7] => sample_057
[8] => sample_058
[9] => sample_059
]
me at phpscott dot com ¶
10 years ago
So, I needed a quick and dirty way to create a dropdown select for hours, minutes and seconds using 2 digit formatting, and to create those arrays of data, I combined range with array merge..
Super simple.
Ray.Paseur often uses Gmail ¶
9 years ago
Interestingly, these two statements produce identical 26-character alphabet arrays.