• PHP Video Tutorials

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.

Parameters

Sr.No Parameter & Description
1

low(Required)

This is a lower range of the array

2

high(Required)

This is a upper range of the array

3

step(Optional)

Steps to increase array element. By default it is 1

Return Value

It returns array of elements.

Example

Try out following example −

<?php
   foreach (range(0, 6) as $number) {
      echo "$number, ";
   }
   print "\n";
   
   foreach (range(0, 100, 10) as $number) {
      echo "$number, ";
   }
   print "\n";
   
   foreach (range('a', 'c') as $letter) {
      echo "$letter, ";
   }
   print "\n";
   
   foreach (range('f', 'a') as $letter) {
      echo "$letter, ";
   }
   print "\n";
?> 

This will produce the following result −

0, 1, 2, 3, 4, 5, 6,
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
a, b, c,
f,e,d,c, b, a
php_function_reference.htm
Advertisements