Hudzilla.org - the homepage of Paul Hudson
Contents > Arrays > Array-specific functions Wish List | Report Bug | About Me ]

5.6.11     Creating an array of numbers: range()

This is NOT the latest copy of this book; click here for the latest version.

array range ( int low, int high [, int step])

Consider this problem:

  • A school has to give all its students an end-of-year examination

  • The test consists of the same 40 questions for each student

  • To minimise cheating, the school wants those 40 questions to be randomly ordered

How would you code that in PHP? The obvious answer is code like this:

<?php
    $questions
= array(1, 2, 3, 4, 5, ..., 38, 39, 40);
    
$questions = shuffle($questions);
?>

The slightly smarter programmer might save themselves some typing with more intelligent code like this:

<?php
    
for ($i = 1; $i <= 40; ++$i) {
        
$questions[] = $i;
    }
    
    
shuffle($questions);
?>

However, that's still not the most efficient way of doing it. Instead, PHP has a special range() function that allows you to create an array of numbers between a low value (parameter one) and a high value (parameter two). So the best way to write the script is this:

<?php
    $questions
= range(1,40);
    
$questions = shuffle($questions);
?>

As you can see, range() returns the array between 1 and 40 (inclusive), ordered sequentially. This is then passed through shuffle() to get the random question numbers - perfect!

There are a three other tricks to range() that you should be aware of. Firstly, it has a third parameter that allows you specify a step amount in the range. For example:

$questions = range(1, 10, 2);
// gives 1, 3, 5, 7, 9

$questions = range(1, 10, 3)
// gives 1, 4, 7, 10

$questions = range(10, 100, 10);

Although the step parameter should always be positive, the second trick is that if your low parameter (parameter one) is higher than your high parameter (parameter two), you get an array counting down, like this:

$questions = range(100, 0, 10);

Finally, you can also use range() to create arrays of characters, like this:

$questions = range("a", "z", 1);
// gives a, b, c, d, ..., x, y, z

$questions = range("z", "a", 2);

For creating these simple kinds of arrays, there is nothing better than using range() - it is very fast, very easy to read, and the step parameter can save you extra work.





<< 5.6.10 Randomising your array: shuffle() and array_rand()   5.7 Multidimensional arrays >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Aaron - 29 Aug 2008

this should work as well:

<pre>
<?php
$questions = range(1,40);
shuffle($questions);

var_dump($questions);

?>
</pre>

Gogo the Great, Kingstown, Serbia - 29 Aug 2008

Tuesday, January 3rd, 2006

The third example above:

<?php
$questions = range(1,40);
$questions = shuffle($questions);
?>

should be rewritten, because line two will in fact overwrite $questions array and turn it into a standard boolean variable with the value of "true".
For the given task, I think the following script would be much more illustrative:

<?php
$questions = range(1,40);
shuffle($questions);

var_export ($questions);
?>



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow