|
|
 |
range (PHP 3 >= 3.0.8, PHP 4, PHP 5) range --
Create an array containing a range of elements
Descriptionarray range ( number low, number high [, number step] )
range() returns an array of elements from
low to high,
inclusive. If low > high, the sequence will be from high to low.
New parameter:
The optional step parameter was added in 5.0.0.
If a step value is given, it will be used as the
increment between elements in the sequence. step
should be given as a positive number. If not specified,
step will default to 1.
Example 1. range() examples |
<?php
foreach (range(0, 12) as $number) {
echo $number;
}
foreach (range(0, 100, 10) as $number) {
echo $number;
}
foreach (range('a', 'i') as $letter) {
echo $letter;
}
foreach (range('c', 'a') as $letter) {
echo $letter;
}
?>
|
|
Note:
Prior to PHP 4.1.0, range() only generated
incrementing integer arrays. Support for character sequences and
decrementing arrays was added in 4.1.0. Character sequence values
are limited to a length of one. If a length greater than one is
entered, only the first character is used.
| Caution |
In PHP versions 4.1.0 through 4.3.2, range() sees
numeric strings as strings and not integers. Instead, they will be
used for character sequences. For example, "4242"
is treated as "4".
|
See also shuffle(),
array_fill(), and
foreach.
User Contributed Notes
range
derek at php dot net
08-May-2005 08: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)) {
for ($i = (float)$low; $i*$sign <= $high*$sign; $i += $step*$sign)
$arr[] = $i;
} else {
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 06: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))
{
$step = ( abs($step)>0 ? abs($step) : 1 ); $dir = ($num1<=$num2 ? 1 : -1); for($i = (float)$num1; $i*$dir <= $num2*$dir; $i += $step*$dir)
{
$temp[] = $i;
}
}
else
{
$num1=ord((string)$num1); $num2=ord((string)$num2);
$step = ( abs($step)>0 ? abs($step) : 1 ); $dir = ($num1<=$num2 ? 1 : -1); for($i = $num1; $i*$dir <= $num2*$dir; $i += $step*$dir)
{
$temp[] = chr($i);
}
}
return $temp;
}
print_r(myRange( 1, 3, 0.5 )); print_r(myRange( "a", "k", 3 )); print_r(myRange( "5", "9" )); print_r(myRange( "!", "%", 1/pi() )); ?>
donwilson at gmail dot com
31-Aug-2004 11: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
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
17-May-2004 09: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
auscoff at no-spam dot yahoo dot com
06-Mar-2002 07:40
If you set number range via variables (eg., range($from, $to)) make sure
you've set type of the variables to integer or your code won't work with version 4.1.0 and above.
$from = 0;
$to = 7;
foreach( range((int) $from, (int) $to) as $number ) {
print "$number\n";
}
| |