range() creates an array of values between a start and an end value. A step lets you skip values or build descending sequences.
$odd = range(1, 9, 2);
print_r($odd);Use a negative step for descending ranges when the start value is greater than the end value.
$output=range($start, $end, [$step]);
Create an array by taking a range starting from $start and ending with $end with optional increment data of $step. The result is $output an array.
$my_array=range(0,10); // create an array
foreach ($my_array as $element) {
echo $element . " ";
}
Output is here
0 1 2 3 4 5 6 7 8 9 10
$my_array=range(0,10,2);
foreach ($my_array as $element) {
echo $element . " ";
}
Ouput is here
0 2 4 6 8 10
$my_array=range(0,100,10);
foreach ($my_array as $element) {
echo $element . " ";
}
Ouput is here
0 10 20 30 40 50 60 70 80 90 100
$my_array=range(100,0,-10);
foreach ($my_array as $element) {
echo $element . " ";
}
100 90 80 70 60 50 40 30 20 10 0
$my_array=range(a,z);
foreach ($my_array as $element) {
echo $element . " ";
}
Output is herea b c d e f g h i j k l m n o p q r s t u v w x y z
$my_array=range(A,H);
foreach ($my_array as $element) {
echo $element . " ";
}
Output is here
A B C D E F G H
$my_array=range(A,H,3);
foreach ($my_array as $element) {
echo $element . " ";
}
Output is here
A D G
$my_array=range(0,10);
shuffle($my_array);
foreach ($my_array as $element) {
echo $element . " ";
}
Output is here
4 5 2 3 7 9 1 8 10 0 6
Above output will change each time you run the script.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.