range

(PHP 3 >= 3.0.8, PHP 4, PHP 5)

range --  建立一个包含指定范围单元的数组

说明

array range ( mixed low, mixed high [, number step] )

range() 返回数组中从 lowhigh 的单元,包括它们本身。如果 low > high,则序列将从 high 到 low。

新参数: 可选的 step 参数是 PHP 5.0.0 新加的。

如果给出了 step 的值,它将被作为单元之间的步进值。step 应该为正值。如果未指定,step 则默认为 1。

例子 1. range() 例子

<?php
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(0, 12) as $number) {
    echo
$number;
}

// The step parameter was introduced in 5.0.0
// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
foreach (range(0, 100, 10) as $number) {
    echo
$number;
}

// Use of character sequences introduced in 4.1.0
// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
foreach (range('a', 'i') as $letter) {
    echo
$letter;
}
// array('c', 'b', 'a');
foreach (range('c', 'a') as $letter) {
    echo
$letter;
}

注: 在版本 4.1.0 之前 range() 函数只产生递增的整数数组。对于字符序列和递减数组的支持是 4.1.0 加入的。字符序列值的长度限定为一。如果输入的长度超过一,则只使用第一个字符。

注意

在 PHP 版本 4.1.0 到 4.3.2 中,range() 将数字字符串看作字符串而不是整数,因此将会被作为字符序列使用。例如,"4242" 会被当作 "4" 来对待。

参见 shuffle()array_fill()foreach


add a note add a note User Contributed Notes
subscription101 at hotmail dot com
08-Jan-2006 10:36
A much simpler way of creating a range of even numbers is by starting with an even number:

<?php

   range
(2, 10, 2);

?>
emory underscore smith at hotmail
21-Aug-2005 10:53
since its not stated explicitly above, thought id point out that you arent limited to using integers.

however, be careful when doing so, as you might not get the range you expect!

to illustrate:

<?php
$am
= range(500,1600,10);
$fm = range(88.1,107.9,.2);
print_r($am);
print_r($fm);
?>

print_r($am) yields the expected result:
            
Array
(
   [0] => 500
   [1] => 510
   [2] => 520
   ...
   [109] => 1590
   [110] => 1600
)

print_r($fm), however, falls a bit (1%) short:

Array
(
   [0] => 88.1
   [1] => 88.3
   [2] => 88.5
   ...
   [97] => 107.5
   [98] => 107.7
)
  
so, if you want to use a non-integral step size params for numeric ranges, be sure to account for fp representation accuracy and error accumulation; a step size of something like pi or 1/10 could spell disaster for a large range. if in doubt, use integral steps and divide ... something like <?php range(88.1,108,.2) ?> might work to recover 107.9, but would not be scalable like, say <?php array_map(create_function('$x','return $x/10;'),range(881,1079,2)) ?>.

-emory
derek at php dot net
08-May-2005 09:13
This should emulate range() a little better.
<?php
function range_wroar($low, $high, $step = 1) {
  
$arr = array();
  
$step = (abs($step)>0)?abs($step):1;
  
$sign = ($low<=$high)?1:-1;
   if(
is_numeric($low) && is_numeric($high)) {
      
//numeric sequence
      
for ($i = (float)$low; $i*$sign <= $high*$sign; $i += $step*$sign)
          
$arr[] = $i;
   }    else    {
      
//character sequence
      
if (is_numeric($low))
           return
$this->range($low, 0, $step);
       if (
is_numeric($high))
           return
$this->range(0, $high, $step);
      
$low = ord($low);
      
$high = ord($high);
       for (
$i = $low; $i*$sign <= $high*$sign; $i += $step*$sign) {
              
          
$arr[] = chr($i);
       }
   }
   return
$arr;
}
?>
j dot gizmo at aon dot at
23-Sep-2004 07:23
i figured i'd add some more functionality to the myRange() functions below.
now you can, besides giving a $step parameter,
1. count backwards
2. count with letters
3. give whatever parameter you want, there's nothing (i know of) that will cause an endless loop (try a negative $step for the previous function....)

<?php
function myRange($num1, $num2, $step=1)
{
   if (
is_numeric($num1) && is_numeric($num2))
   {
      
//we have a numeric range
      
$step = ( abs($step)>0 ? abs($step) : 1 ); //make $step positive
      
$dir = ($num1<=$num2 ? 1 : -1); //get the direction
      
for($i = (float)$num1; $i*$dir <= $num2*$dir; $i += $step*$dir)
       {
          
$temp[] = $i;
       }
   }
   else
   {
      
//we have a character range
      
$num1=ord((string)$num1); //convert to ascii value
      
$num2=ord((string)$num2);
      
$step = ( abs($step)>0 ? abs($step) : 1 ); //make $step positive
      
$dir = ($num1<=$num2 ? 1 : -1); //get direction
      
for($i = $num1; $i*$dir <= $num2*$dir; $i += $step*$dir)
       {
          
$temp[] = chr($i);
       }
   }
   return
$temp;
}

print_r(myRange( 1, 3, 0.5 )); //you can use fractional steps
print_r(myRange( "a", "k", 3 )); //or count letters
print_r(myRange( "5", "9" )); //numbers are detected even if hidden in strtings
print_r(myRange( "!", "%", 1/pi() )); //or mess around with senseless parameters

?>
donwilson at gmail dot com
01-Sep-2004 12:38
To speed your MyRange() function, I have created a much nicer function with less code to sift through to include the step parameter.

<?php   
  
// range() limitation for PHP <5.0.0
  
function myRange($num1, $num2, $step=1)
   {
       for(
$i = $num1; $i <= $num2; $i += $step)
       {
          
$temp[] = $i;
       }
      
       return
$temp;
   }
?>

For whatever reason my comment was deleted..?
Forrester at tfcustomized dot com
18-May-2004 10:57
Since users of < PHP 5.0.0 don't have the option of the step parameter, I've created a little function to account for it:

@USAGE: (int low, int high [, int step])

function myRange($low,$high,$step=1)
{
$ranArray = range($low,$high);
$step--;
$keys = count($ranArray);
   for($i=0;$i<$keys;$i++)
   {
   $retArray[] = $ranArray[$i];
   $i = $i + $step;
   }
return $retArray;
}

// Example usage:
print_r(myRange(1,11,2));
// Returns the array:
// [0] => 1
// [1] => 3
// [2] => 5
// [3] => 7
// [4] => 9
// [5] => 11