PHP - Function range()


Advertisements


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.

Parameters

Sr.No Parameter & Description
1

low

Required. Lower range of the array

2

high

Required. Upper range of the array

3

step

Optional. 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, ";
   }
   print "\n";
   
   foreach (range(0, 100, 10) as $number) {
      echo "$number, ";
   }
   print "\n";
   
   foreach (range('a', 'i') as $letter) {
      echo "$letter, ";
   }
   print "\n";
   
   foreach (range('c', 'a') as $letter) {
      echo "$letter, ";
   }
   print "\n";
?> 

This will produce the 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

php_function_reference.htm

Advertisements