How to use PHP range() function productively

by  
Share:
php range function

It might come as a surprise to you, but did you know the things you can do with the PHP range function. You can get a range of values in an array with just one line of code. It can also be used to generate even/odd numbers, multiples of tables, alphabets list, etc.

PHP range(start, end, step); create an array containing a range of elements. you can read more about this function on their official documentation.

Get range from A to Z in array

Using the range function we put the starting value as A and ending 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 array

Similar to problem above, just change the parameter values to get the desired range range.

<?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 range from 0 to 100 in array

To get the range between any numbers, just put the starting and ending value of your desired range.

<?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 even numbers range from 0 to 100 in array

Similar to the problem above, we will add the third parameter in the PHP range function. We start the range from 2 and increase the value by 2 till the end of the range.

<?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 )
?>

Get odd numbers range from 0 to 100 in array

In this case, we start the range from 1 and increase the value by 2 till the end of the range.

<?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 multiples of 5 between 0 to 100 from in array

If you want to get the range of numbers in their multiples, take that number in the third/step parameter. In our case, we start the range from 0 and increase the value by 5 till the end of the range.

<?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)
?>

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.