Monday 26 August 2013

PHP Function range()

Syntax

range ( $low, $high [, $step] );

Definition and Usage

This function returns an array of elements from low to high, inclusive. If low > high, the sequence will be from high to low.
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.

Paramters

ParameterDescription
lowRequired. Lower range of the array
highRequired. Upper range of the array
stepOptional. Steps to increase array element. By default it is 1

Return Value

Array of elements.

Example

Try out following example:
<?php
foreach (range(0, 12) as $number) {
    echo "$number, ";
}
echo "<br />";
foreach (range(0, 100, 10) as $number) {
    echo "$number, ";
}
echo "<br />";
foreach (range('a', 'i') as $letter) {
    echo "$letter, ";
}
echo "<br />";
foreach (range('c', 'a') as $letter) {
    echo "$letter, ";
}
echo "<br />";
?> 
This will produce following result:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
a, b, c, d, e, f, g, h, i
c, b, a

No comments:

Post a Comment