range(): Creating array with a range of elements

$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.

Example

$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

Example with step value

$my_array=range(0,10,2); 
foreach ($my_array as $element) {
    echo $element . "  ";
}
Ouput is here
0 2 4 6 8 10

Example with step value

$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

Example with number decrement step value

$my_array=range(100,0,-10); 
foreach ($my_array as $element) {
  echo $element . "  ";
}
100 90 80 70 60 50 40 30 20 10 0

Example using alphabets

$my_array=range(a,z); 
foreach ($my_array as $element) {
  echo $element . "  ";
}
Output is here
a 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

Example using alphabets

$my_array=range(A,H); 
foreach ($my_array as $element) {
  echo $element . "  ";
}
Output is here
A B C D E F G H

Example Using alphabets and step

$my_array=range(A,H,3); 
foreach ($my_array as $element) {
  echo $element . "  ";
}
Output is here
A D G

Range with shuffle()

The array created by using range() maintains an order, either increment or decrement. If you want to randomize the elements by not maintaining any particular order then we can use shuffle() function.
$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.
Joining Two Arrays by array_merge
Array REFERENCE
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer