W3docs

range()

Array range is a useful PHP function that allows you to generate a range of elements in an array. It's a very powerful function that can save you a lot of time

Introduction

range() is a built-in PHP function that builds an array containing a sequence of elements from a start value to an end value. Instead of writing a loop to push numbers or letters into an array one by one, you describe the endpoints (and optionally a step) and PHP fills in everything in between.

This page covers the syntax, every kind of range you can generate (ascending, descending, integers, floats, and characters), the gotchas that trip people up, and where range() fits in real code.

Syntax

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

Where:

  • $start: The first value of the sequence.
  • $end: The last value of the sequence.
  • $step: Optional. The amount added between values. Default is 1. It must be a positive number — range() decides the direction from $start and $end, not from the sign of the step.

Two things are worth memorizing up front:

  • Both ends are included. range(0, 5) returns six elements (0 through 5), not five.
  • The result is always a new, zero-indexed array — the keys are 0, 1, 2, … regardless of the values inside.

Examples

Generate numbers from 0 to 5

The simplest call takes a start and an end. Note that 5 is included in the result:

php— editable, runs on the server

Output:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)

Use negative numbers

$start and $end can be negative, and the sequence still steps by 1:

php— editable, runs on the server

Output:

Array
(
    [0] => -5
    [1] => -4
    [2] => -3
    [3] => -2
    [4] => -1
    [5] => 0
    [6] => 1
    [7] => 2
    [8] => 3
    [9] => 4
    [10] => 5
)

Step over values

The third argument sets the increment. Here it is 2, so only even numbers are produced:

php— editable, runs on the server

Output:

Array
(
    [0] => 0
    [1] => 2
    [2] => 4
    [3] => 6
    [4] => 8
    [5] => 10
)

Generate a range of characters

When $start and $end are single characters, range() walks the alphabet between them. This is handy for building A–Z menus, column labels, or test fixtures:

php— editable, runs on the server

Output:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [16] => q
    [17] => r
    [18] => s
    [19] => t
    [20] => u
    [21] => v
    [22] => w
    [23] => x
    [24] => y
    [25] => z
)

Count down with a descending range

If $start is greater than $end, range() counts down automatically. You do not pass a negative step — the step stays positive:

<?php

$countdown = range(5, 1);
print_r($countdown);

Output:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

Use a float step

The step can be a float, which lets you build fractional sequences — useful for sliders, chart axes, or sampled values:

<?php

$fractions = range(0, 1, 0.25);
print_r($fractions);

Output:

Array
(
    [0] => 0
    [1] => 0.25
    [2] => 0.5
    [3] => 0.75
    [4] => 1
)

Common gotchas

  • The range is inclusive. range(1, 3) gives [1, 2, 3]. If you expected a length-of-$end result, you'll be off by one.
  • Keep the step positive. The direction comes from the order of $start and $end. Passing a negative step (for example range(0, 5, -1)) is an error — older PHP emitted a warning, while PHP 8.3+ throws a ValueError. A step of 0 is invalid as well.
  • The last element may be skipped. range(0, 10, 3) yields [0, 3, 6, 9]10 is not reachable from 0 in steps of 3, so the sequence stops at the last value that fits.
  • Floats can be imprecise. Because floating-point numbers can't represent every decimal exactly, a step like 0.1 may not land on the value you expect. Round the results, or step over integers and divide afterward, when exactness matters.
  • Memory grows with size. range(0, 1000000) builds a one-million-element array in memory. If you only need to iterate, a for loop or SplFixedArray avoids the allocation.

When to use range()

range() shines whenever you need a ready-made sequence rather than an empty container:

  • Driving a loop. Combine it with foreach to iterate over a fixed span: foreach (range(1, 12) as $month) { … }.
  • Building option lists — years, days of the month, or AZ letters for a dropdown.
  • Pairing with array_map() to transform each value, for example array_map(fn($n) => $n * $n, range(1, 5)) for squares.

If instead you want an array of the same repeated value, reach for array_fill() rather than range(). For more on arrays in general, see the PHP arrays guide.

Summary

range() creates a zero-indexed array of sequential values between two endpoints. It works with integers, floats, and characters, counts up or down, and accepts an optional step. Remember that both ends are inclusive, the step is always positive, and large ranges allocate real memory — and it becomes one of the most convenient one-liners in PHP.

Practice

Practice
What does the range() function in PHP do?
What does the range() function in PHP do?
Was this page helpful?