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.


plus2net.com




Post your comments , suggestion , error , requirements etc here .




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