Content · Programming · PHP

PHP range() Function: Usage and Examples

Learn PHP range() through clear code examples, practical usage, important limitations and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

range() generates a sequence of values between two endpoints with an optional step. This guide explains the function with focused examples and the decisions that matter in real applications.

What Is range() in PHP?

Generates a sequence of values between two endpoints with an optional step.

A typical call looks like this:

<?php

range($start, $end, [$step]);

The input arguments shown in the call identify the relevant connection, resource, value, path or configuration for the operation. Pay attention to argument order and to the return value demonstrated below.

Example 1: Basic Usage

This first example uses range() directly to demonstrate its basic purpose.

<?php

print_r(range(1, 5));

Output:

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

The returned value illustrates generates a sequence of values between two endpoints with an optional step. A zero step is invalid; large ranges can allocate substantial memory.

Example 2: A Different Input or Option

The next example changes the input or option, showing another supported use of range().

<?php

print_r(range(10, 2, 2));

Output:

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

The returned value illustrates generates a sequence of values between two endpoints with an optional step. A zero step is invalid; large ranges can allocate substantial memory.

Practical Example: Applying the Function

In application code, range() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.

<?php

$days = range(1, 7); foreach ($days as $dayNumber) echo "Day $dayNumber" . PHP_EOL;

Output:

Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7

The returned value illustrates generates a sequence of values between two endpoints with an optional step. A zero step is invalid; large ranges can allocate substantial memory.

Important Behavior and Edge Cases

  • Main behavior: Generates a sequence of values between two endpoints with an optional step.
  • Important limitation: A zero step is invalid; large ranges can allocate substantial memory.
  • Requirements: This operation is available in standard PHP; validate inputs and handle failures where relevant.

Comparison With Related Functions

Function or ApproachWhen to Use It
range()Generates a sequence of values between two endpoints with an optional step.
array_fillCreates an array of repeated values, whereas range() generates a sequence.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the specific limits or preconditions of range().A zero step is invalid; large ranges can allocate substantial memory.
Assuming every environment produces the same result.This operation is available in standard PHP; validate inputs and handle failures where relevant.

Key Takeaways

  • Generates a sequence of values between two endpoints with an optional step.
  • A zero step is invalid; large ranges can allocate substantial memory.
  • This operation is available in standard PHP; validate inputs and handle failures where relevant.

Use range() when its behavior matches your task, and verify the relevant return value before taking further action. The output depends on the local configuration when it uses resources, files or an interactive terminal.

Share this resourceSend this page to someone who may find it useful.