PHP range

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: range() function

PHP: Create an array containing a range of elements

The range() function used to create an array containing a range of elements.

Version

(PHP 4 and above)

Syntax

range(low_value, high_value, step)

Parameters

Name

Description

Required /
Optional

Type

low_value

Specify the lowest value.

Required

Mixed*

high_value

Specify the highest value.

Required

Mixed*

step

It is used as increment between elements. The default value is 1.

Optional

Integer/Float

*Mixed : Mixed indicates multiple (but not necessarily all) types.

Return value

An array.

Value Type : Array

Pictorial Presentation :

php array range() function

Example :

  1. $number_list = range(11,19);  

  2. print_r ($number_list);  

  3. echo "
     ";  

  4. $number_list_step = range(11,19,2);  

  5. print_r ($number_list_step);  

  6. echo "
     ";  

  7. $letter_list = range("u","z");  

  8. print_r ($letter_list);  

  9. ?>  

Output :

Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 [4] => 15 [5] => 16 [6] => 17 [7] => 18 [8] => 19 )
Array ( [0] => 11 [1] => 13 [2] => 15 [3] => 17 [4] => 19 )
Array ( [0] => u [1] => v [2] => w [3] => x [4] => y [5] => z )