Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

range

Creates an array holding a sequence of values.

Syntax

range(string|int|float $start, string|int|float $end, int|float $step = 1): array

Parameters

ParameterTypeDescription
$startstring|int|floatThe first value.
$endstring|int|floatThe last value, inclusive.
$stepint|floatOptional. The step; 1 by default.

Return value

arrayAn array of the values from start to end.

Examples

Numbers, and numbers with a step:

<?php
 
echo implode(', ', range(1, 5)), "\n";
echo implode(', ', range(0, 100, 25));

Output:

1, 2, 3, 4, 5
0, 25, 50, 75, 100

Reverse order, and letters:

<?php
 
echo implode(', ', range(5, 1)), "\n";
echo implode('', range('a', 'e'));

Output:

5, 4, 3, 2, 1
abcde

Notes

Both bounds are included. For very long sequences a generator is more economical.

See also

  • array_fill — Creates an array of identical values.
  • array_map — Applies a function to every element of an array.

Related recipes

In the tutorial

  • Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.

Thanks for reading!

Donate to the site — we work hard for you.