PHP 8 reference · Array Functions

PHP range() Function

Create an array of numbers or letters from a start to an end value, with an optional step, such as range(1, 10, 2).

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
start The first value.
end The last value (included if the step lands on it).
step optional The increment; must not be 0. Default: 1

Return value

An array of values.

Examples

NumbersTry it yourself
<?php
echo implode(",", range(1, 5)), "\n";
echo implode(",", range(0, 20, 5)), "\n";
echo implode(",", range(5, 1)), "\n";

Output

1,2,3,4,5
0,5,10,15,20
5,4,3,2,1
Letters and decimalsTry it yourself
<?php
echo implode("", range("a", "f")), "\n";
echo implode(",", range(0, 1, 0.25)), "\n";

Output

abcdef
0,0.25,0.5,0.75,1

Version history

VersionChange
PHP 4.0Added
PHP 8.3A float step that's a whole number gives integers, and a step of 0 throws a ValueError with a clearer message.

In other languages

Was this page helpful?