PHP range() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, range() creates an array that contains a sequence of numbers or letters, from a start value to an end value.

Instead of typing [1, 2, 3, 4, 5] yourself, you write range(1, 5). It works with whole numbers, decimal numbers and single letters, and the optional step controls how big each jump is.

Syntax

Call range() with the values you need. Start with this line.

PHP
$numbers = range(1, 5);
CallWhat it does
range(1, 5)Returns [1, 2, 3, 4, 5].
range("a", "e")Returns the letters a, b, c, d and e.
range(0, 10, 5)Returns [0, 5, 10] because each step adds 5.

The end value is included. If start is bigger than end, the sequence counts down. The step should be written as a positive number, because PHP decides the direction by itself.

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

Parameters

ParameterDescription
startRequired. The first value of the sequence.
endRequired. The last value. It is included when the step reaches it exactly.
stepOptional. How much to add each time. The default is 1.

Return Value

Return ValueDescription
arrayAn array with the values from start to end, going up or down by the step.

PHP Examples

Example 1 — Create Number Sequences

The simplest use is a run of whole numbers. A step changes the gap between values.

PHP
<?php
echo implode(", ", range(1, 5)), PHP_EOL;
echo implode(", ", range(0, 10, 5)), PHP_EOL;

How It Works

  1. range(1, 5) builds an array with the numbers from 1 to 5.
  2. implode() joins the values with a comma and a space to make a readable line.
  3. range(0, 10, 5) adds 5 each time, so the values are 0, 5 and 10.
  4. The end value is included because the step lands on it exactly.

Example 2 — Letters and Counting Down

range() also works with letters, and it counts down when the start is larger than the end.

PHP
<?php
echo implode(", ", range("a", "e")), PHP_EOL;
echo implode(", ", range(5, 1)), PHP_EOL;
echo implode(", ", range(10, 0, 5)), PHP_EOL;

How It Works

  1. range("a", "e") walks through the alphabet from a to e.
  2. range(5, 1) starts higher than it ends, so PHP counts down automatically.
  3. range(10, 0, 5) also counts down, using a positive step of 5.
  4. implode() prints each array on its own line.

Example 3 — Decimal Steps and Counting Values

A step with decimals gives decimal values. count() tells you how many values were created.

PHP
<?php
echo implode(", ", range(0, 1, 0.25)), PHP_EOL;
echo count(range(1, 100)), PHP_EOL;

How It Works

  1. A step of 0. 25 gives the values 0, 0.25, 0.5, 0.75 and 1.
  2. implode() prints the numbers on one line.
  3. range(1, 100) builds an array with 100 numbers.
  4. count() returns the size of that array, which is 100.

Frequently Asked Questions

It creates an array containing a sequence of numbers or letters from a start value to an end value, with an optional step.
Yes, as long as the step reaches it exactly. For example range(1, 10, 3) gives 1, 4, 7, 10, but range(1, 9, 3) gives 1, 4, 7.
Yes. If the start is larger than the end, for example range(5, 1), it returns 5, 4, 3, 2, 1. Keep the step positive.
Yes. range("a", "z") returns all lowercase letters and range("A", "Z") returns all capital letters.
Not for very large numbers because it builds the full array in memory. A normal for loop uses almost no memory.

Next: reset()

reset() moves the internal array pointer back to the first element and returns the value o.

reset() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful