Instantly create an array full of values in a range of your choice. For example, all the numbers five through twenty, or the letters B through N, or maybe just the odd numbers zero through fifty.

You specify the starting point, the ending point, and optionally the step (defaults to one). If the start is greater than the end, the array will be filled “backwards”.

Only the first character of the strings will be used when creating a range of characters. Any other characters of the string will be ignored.

$arr = range(1, 8);    // array(1, 2, 3, 4, 5, 6, 7, 8)
$arr = range(8, 1);    // array(8, 7, 6, 5, 4, 3, 2, 1)
$arr = range(1, 8, 2); // array(1, 3, 5, 7)
$arr = range(1, 8, 5); // array(1, 6)
 
$arr = range('q', 'y');    // array('q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y')
$arr = range('q', 'm');    // array('q', 'p', 'o', 'n', 'm')
$arr = range('q', 'y', 2); // array('q', 's', 'u', 'w', 'y')
$arr = range('q', 'y', 5); // array('q', 'v')
 
// this function appears to take the
// absolute value of the step parameter
$arr = range(1, 8, -2); // array(1, 3, 5, 7)

Take care mixing integers and strings. The string will first be converted to a numeric value. Also, the step need not be a whole number. However, if the step is not a whole number, any strings used will be converted to numeric.

If the string is converted to a numeric value, the “only the first character” rule is out. That rule is applied after any conversion.

$arr = range('1', '8'); // array(1, 2, 3, 4, 5, 6, 7, 8)
$arr = range(2, 'x');   // array(2, 1, 0)
$arr = range(2, '5x');  // array(2, 3, 4, 5)
 
$arr = range(2, 4, .5);      // array(2, 2.5, 3, 3.5, 4)
$arr = range(3.14, 5.2, .3); // array(3.14, 3.44, 3.74, 4.04, 4.34, 4.64, 4.94, 5.24)
$arr = range('a', 'g', .5);  // array(0);
 
$arr = range('28', '33'); // array(28, 29, 30, 31, 32, 33)
$nums = range(1, 69);
shuffle($nums);
echo "Your winning lottery numbers are:
{$nums[0]} {$nums[1]} {$nums[2]} {$nums[3]} {$nums[4]} {$nums[5]}";