PHP/Utility Function/range

Материал из Web эксперт
Перейти к: навигация, поиск

If your low parameter (parameter one) is higher than your high parameter (parameter two), you get an array counting down, like this:

 
<?
    $questions = range(100, 0, 10);
    // gives 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0
?>



Initializing an Array As a Range or Sequence of Values: array range(mixed $start, mixed $end[, mixed $step])

 
<?php 
function array_list($array) { 
    printf("<p>(%s)</p>\n", implode(", ", $array) ); 
} 
$arr1 = range(5, 11); 
array_list($arr1); 
$arr2 = range(0, -5);
array_list($arr2); 
$arr3 = range(3, 15, 3);
array_list($arr3); 
array_list( range(20, 0, -5) ); 
array_list( range(2.4, 3.1, .1) ); 
array_list( range("a", "f") ); 
array_list( range("M", "A", -2) ); 
?>



range( ) function creates an array of numbers between a low value and a high value.

 
Get an array of the sequential numbers between 1 and 40 (inclusive)
<?
    $numbers = range(1,40);
    print_r($numbers);
?>



range( ) function has a third parameter that allows you specify a step amount in the range.

 
//array range ( mixed low, mixed high [, number step] )
<?
    $questions = range(1, 10, 2);
    // gives 1, 3, 5, 7, 9
    $questions = range(1, 10, 3)
    // gives 1, 4, 7, 10
    $questions = range(10, 100, 10);
    // gives 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
    $float = range(1, 10, 1.2);
    // gives 1, 2.2, 3.4, 4.6, 5.8, 7, 8.2, 9.4
?>



Use range( ) to create arrays of characters, like this:

 
<?
    $questions = range("a", "z", 1);
    // gives a, b, c, d, ..., x, y, z
    $questions = range("z", "a", 2);
    // gives z, x, v, t, ..., f, d, b
?>