PHP range() Function

In this chapter you will learn:

  1. Definition for PHP range() Function
  2. Syntax for PHP range() Function
  3. Parameter for PHP range() Function
  4. Note for PHP range() Function
  5. Return value for PHP range() Function
  6. Example - Use range() function to create array
  7. Example - Letter range
  8. Example - Create an array containing a range of elements from "0" to "5"
  9. Example - Return an array of elements from "0" to "50" and increment by 10
  10. Example - Using letters - return an array of elements from "a" to "d"

Definition

The range() function creates an array of numbers between the first parameter and the second parameter two.

Syntax

PHP range() Function has the following syntax.

range(low,high,step)

Parameter

ParameterIs RequiredDescription
lowRequired.Lowest value of the array
highRequired.Highest value of the array
stepOptional.Increment used in the range. Default is 1.

Note

If the low parameter is higher than the high parameter, the range array will be from high to low.

Return

This function returns an array of elements from low to high.

Example 1

Use range() function to create array


<?PHP
$numbers = range(1,10);
print_r($numbers);
?>

The code above generates the following result.

The third parameter from range() function sets the step. It can either be an integer or a floating-point number.


<?PHP/*from  jav a 2  s  .  c  om*/
$numbers = range(1, 10, 2);
print_r($numbers);
print "\n";
$numbers = range(1, 10, 3);
print_r($numbers);
print "\n";
$numbers = range(10, 100, 10);
print_r($numbers);
print "\n";
$numbers = range(1, 10, 1.2);
print_r($numbers);
?>

The code above generates the following result.

Example 2

The third parameter should always be positive.

If the first parameter is higher than the second parameter, we will get an array counting down.


<?PHP/*from  j  a v a 2s .  com*/
$values = range(100, 0, 10);
print_r($values);

//We can use range() to create arrays of characters:

$values = range("a", "z", 1);
print_r($values);
print "\n";


$values = range("z", "a", 2);
print_r($values);
?>

The code above generates the following result.

Example 3

Create an array containing a range of elements from "0" to "5":


<?php
$number = range(0,5);
print_r ($number);
?>

The code above generates the following result.

Example 4

Return an array of elements from "0" to "50" and increment by 10.


<?php
$number = range(0,50,10);
print_r ($number);
?>

The code above generates the following result.

Example 5

Using letters - return an array of elements from "a" to "d"


<?php
$letter = range("a","d");
print_r ($letter);
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP reset() Function
  2. Syntax for PHP reset() Function
  3. Parameter for PHP reset() Function
  4. Return from PHP reset() Function
  5. Related methods for PHP reset() Function
  6. Example - Using current and next and reset
  7. Example - A demonstration of all related methods
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function