Jump to: navigation, search

Php range

From w3cyberlearnings

Contents

PHP function range

This function creates a new array containing a range of elements.

Syntax range

  • start: lowest value to start
  • end: highest value to end
  • step (optional): the increment uses between the start and end range. Default value is 1
range(start,end, step);

Example 1: Default step

  • Create a new array, and its values start from 40 to 62.
<?php
$number = range(40,62);
print_r($number);
?>

Output

Array
(
    [0] => 40
    [1] => 41
    [2] => 42
    [3] => 43
    [4] => 44
    [5] => 45
    [6] => 46
    [7] => 47
    [8] => 48
    [9] => 49
    [10] => 50
    [11] => 51
    [12] => 52
    [13] => 53
    [14] => 54
    [15] => 55
    [16] => 56
    [17] => 57
    [18] => 58
    [19] => 59
    [20] => 60
    [21] => 61
    [22] => 62
)

Example 2: Use step

<?php
$number = range(40,62,3);
print_r($number);
?>

Output

Array
(
    [0] => 40
    [1] => 43
    [2] => 46
    [3] => 49
    [4] => 52
    [5] => 55
    [6] => 58
    [7] => 61
)

Example 3

<?php
$test = range('A','Z',2);
print_r($test);
?>

Output

Array
(
    [0] => A
    [1] => C
    [2] => E
    [3] => G
    [4] => I
    [5] => K
    [6] => M
    [7] => O
    [8] => Q
    [9] => S
    [10] => U
    [11] => W
    [12] => Y
)

Related Links


Navigation
Web
SQL
MISC
References