In this article, you will learn how to create an array that should contain a series of elements within the given range. The PHP range() function fills and returns an array of elements within the specified range.
The elements are filled in the order of low to high. However, if the low parameter is greater than the high parameter, the array will be filled in the order of high to low.
What is the syntax of the range() function in PHP?
range(low, high, step)
Parameter | Description |
---|---|
low | The least value of the array – Required |
high | The highest value of the array – Required |
step | The increment between each value within the range (by default = 1)- Optional |
Examples of range() function
Example 1. Create an array containing elements from 1 to 10.
<?php
$n= range(1,10);
print_r ($n);
?>
Example 2. Create an array containing elements from 10 to 100 with a gap of 10 between each of them.
<?php
$n= range(10,100, 10);
print_r ($n);
?>
Example 3. Create an array containing elements from “a” to “s”
<?php
$q= range("a","s");
print_r ($q);
?>