With the php range() function, we can create arrays of elements in a range of numbers and letters.

$arr = range(0,10);

print_r($arr);

//Output:
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
)

Creating a range of letters is easy as well with the php range() function.

$arr = range("a","d");

print_r($arr);

//Output:
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

You can also create a range that steps with an increment or decrement between elements in the sequence.

$arr = range(0,8,2);

print_r($arr);

//Output:
Array
(
    [0] => 0
    [2] => 2
    [4] => 4
    [6] => 6
    [8] => 8
)

When working in php, being able to easily create arrays of elements is very useful. One such case is when we want to generate an array of elements in a range of numbers or letters.

We can create an array of consecutive numbers or letters with the php range() function.

The range() function takes in 3 arguments. The first argument is the starting point, the second argument is the ending point, and the third argument is the step size.

range() returns an array of elements from the starting point to ending point, inclusive of both points.

For example, we can create an array with the numbers from 1 to 10 with php range() function.

$arr = range(0,10);

print_r($arr);

//Output:
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
)

Using php range() Function to Create an Array with a Range of Letters

With range(), we can also create an array of letters in a range. Just like with numbers, we pass a starting point and an ending point.

range() will start at a particular letter and add elements until the ending letter.

Below is a simple example of creating an array of the letters from ‘a’ to ‘d’.

$arr = range("a","d");

print_r($arr);

//Output:
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

Using range() to Create an Array with a Range of Numbers with Step

range() takes an optional third parameter called ‘step’ which allows us to increment the elements in the range by different numbers.

The default is to increment by 1, but if you want to step by 2 or more, then you can pass another number.

For example, if we want to create an array of even numbers in a range, we start with an even number and step by 2.

Below is an example in php for how to create an array of even numbers with range().

$arr = range(0,8,2);

print_r($arr);

//Output:
Array
(
    [0] => 0
    [1] => 2
    [2] => 4
    [3] => 6
    [4] => 8
)

If you want to create an array of odd numbers in a range, you can start with an odd number and step by 2.

Below is an example in php for how to create an array of odd numbers with range().

$arr = range(1,9,2);

print_r($arr);

//Output:
Array
(
    [0] => 1
    [1] => 3
    [2] => 5
    [3] => 7
    [4] => 9
)

Hopefully this article has been useful for you to use the php range() function to create an array of elements in a range given a starting and ending position.

Categorized in:

PHP,

Last Update: February 26, 2024