6 Productive ways to use the PHP range() function

by  
Share:
php range function

It might come to you as a surprise, but the PHP range function can do just more than returning an array with a range of elements. If you use it well, you can get even/odd numbers, multiples of tables, alphabets list, in an array with just one line of code.

Before getting started, let us understand what the PHP range function does?

The PHP range function range(start, end, step); create an array containing a range of elements.

It accepts three parameters, the first and second is the start and end of the sequence. The third parameter is optional, but if provided, will be used as the increment between elements in the sequence.

You can read more about this function at their official documentation.

Let us see where can the PHP range function prove to be productive for us.

Get range from 0 to 100 in an array

This is what the PHP range function is designed for. You set the first parameter as 0 and the second value as 100. It will return an array containing the value from 0-100. Change the first and last value as per your choice.

<?php
$numbersArray = range(0, 100); 
print_r($numbersArray);

output
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 ...[97] => 98 [98] => 99 [99] => 100 )
?>

Get multiples of 5 between 0 to 100 in an array

If you want to get the range of numbers in multiples, the third parameter comes into play. In our case, the first parameter will be 0, the second will be 100 and the third parameter will be 5. Just change the third parameter to get its multiples.

I have also written a tutorial to create months & year dropdown list using PHP range function.

<?php
$numbersArray = range(0, 100, 5); 
print_r($numbersArray);

output
Array ( [0] => 0 [1] => 5 [2] => 10 [3] => 15 [4] => 20 [5] => 25 [6] => 30 ...[18] => 90 [19] => 95 [20] => 100)
?>

Generate even numbers range from 0 to 100 in an array

If you need to get a list of even numbers between a predefined range, simply use the PHP range function. Provide the starting and ending value of your range, and then set 2 as the third parameter.

P.S: The starting range value has to be an even number.

<?php
$numbersArray = range(2, 100, 2); 
print_r($numbersArray);

output
Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 [5] => 12 [6] => 14 ...[47] => 96 [48] => 98 [49] => 100 )
?>

Generate odd numbers range from 0 to 100 in an array

Similar to the even numbers problem, use the exact code. Just make sure that the starting range value is an odd number.

<?php
$numbersArray = range(1, 100, 2); 
print_r($numbersArray);

output
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 [5] => 11 [6] => 13 ...[47] => 95 [48] => 97 [49] => 99)
?>

Get range from A to Z in an array

Use the range function to create an array containing values from A-Z. Set the first parameter as A and the second value as Z.

<?php
$upperCaseArray = range('A', 'Z'); 
print_r($upperCaseArray);

output
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G ..... ..... [23] => X [24] => Y [25] => Z )
?>

Get range from a to z in an array

Similar to the situation above, if you need the value in a lower case, change the first parameter as a and the second value as z.

<?php
$lowerCaseArray = range('a', 'z'); 
print_r($numbersArray);

output
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ..... ..... [24] => y [25] => z )
?>

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.