PHP range Function: Create Arrays with Sequence Values

php range function

The PHP range function helps you make sequences. Without it, you would write loops every time you need numbers or letters in order.

Understand the PHP range Function

The range function in PHP makes an array with values that follow a sequence.
It works with numbers and letters in the same way.

Here is the syntax:

range(start, end, step)
  • start is the first value.
  • end is the last value.
  • step is how much to add or subtract between values.

The function returns an array. You can loop through it or use it in other functions.

If you want a list of numbers from 1 to 10 or letters from A to Z, range does it in one line.

Here is a quick example:

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

This prints:

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

The function starts at 1, ends at 5, and adds 1 each time by default.

Examples of range in PHP

Create Number Sequences:

$numbers = range(10, 15);
print_r($numbers);

This gives 10, 11, 12, 13, 14, and 15 in one array. The output is an array:

Array
(
[0] => 10
[1] => 11
[2] => 12
[3] => 13
[4] => 14
[5] => 15
)

Create Letter Sequences:

$letters = range('A', 'F');
print_r($letters);

You get A, B, C, D, E, and F:

Array
(
[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
[5] => F
)

Set Step Values in range:

$evens = range(2, 10, 2);
print_r($evens);

This shows 2, 4, 6, 8, and 10:

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

Combine range with a foreach Loop:

foreach (range(1, 3) as $num) {
    echo "Number: $num\n";
}

This prints numbers with text before them. The output:

Number: 1
Number: 2
Number: 3

Use range for Date Ranges:

$dates = range(strtotime("2025-08-01"), strtotime("2025-08-05"), 86400);

foreach ($dates as $date) {
    echo date("Y-m-d", $date) . "\n";
}

This makes dates from August 1 to August 5, one day apart. The output:

2025-08-01
2025-08-02
2025-08-03
2025-08-04
2025-08-05

Generate Reverse Sequences with range:

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

This returns 5, 4, 3, 2, and 1:

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

Filter Data from a range Output:

$nums = range(1, 10);
$odd = array_filter($nums, function($n) { return $n % 2 != 0; });
print_r($odd);

This keeps only odd numbers. Here is the output:

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

Combine range with Array Functions:

$nums = range(1, 5);
$sum = array_sum($nums);
echo $sum; // 15

This adds the numbers and shows the total.

Wrapping Up

In this article, you learned what the PHP range function does and how to use it in many ways.

Here is a quick recap:

  • The range function makes arrays with numbers or letters in order
  • It can move in steps and work with loops and array functions
  • It helps you find data

FAQs

How to create a sequence of numbers using range in PHP?

You can create a number sequence with range. Example:
$numbers = range(1, 5);  
print_r($numbers);
This will output an array from 1 to 5.

How to generate letters from A to Z using range in PHP?

Use range with character parameters. Example:
$letters = range('A', 'Z');  
print_r($letters);
This will output an array from A to Z.

How to set steps between values in range in PHP?

Pass a step value as the third parameter in range. Example:
$evens = range(0, 10, 2);  
print_r($evens);
This creates an array with values 0, 2, 4, 6, 8, 10.

How to reverse a sequence with range in PHP?

Pass higher value first in range. Example:
$reverse = range(5, 1);  
print_r($reverse);
This will output an array from 5 to 1.

Similar Reads

How to Delete Data in PHP MySQL

In this tutorials, we are going to explain the PHP MySQL Delete Data process with a simple, unique guide. By…

PHP fread Function: How to Read Files in PHP

In some cases, you need to handle a lot of data or simply try to open a file, or read…

PHP List MongoDB Collections

Sometimes, you may need to list collections with MongoDB in a PHP environment to manage or analyze your database structure.…

PHP Assign Function to Variable – Examples and Explanation

PHP variable function is a variable that is used to assign a function name with parentheses (). However, the PHP interpreter…

PHP Syntax: Standard Tags, Echo, and Short Tags

Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…

Polymorphism in PHP: How It Works with Examples

PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…

PHP array_rand() Function: Usage & Examples

The array_rand() is a helper function in PHP that makes it easier when dealing with arrays; you can just pull…

PHP array_merge_recursive: Merge Arrays Deeply

PHP array_merge_recursive joins two or more arrays into one nested array and keeps all values with their keys. Syntax of…

PHP array_diff_uassoc: How to Compare Arrays with Keys

PHP array_diff_uassoc compares arrays by values and keys. You can also pass a custom function to compare keys in your…

PHP is_dir Function: Check if a Directory Exists

Sometimes, when you run a script, you may encounter an error because the directory you're trying to access doesn't exist.…

Previous Article

HTML Skip Link Guide for Beginners and Web Developers

Next Article

HTML Elements and Tags: Elements vs Tags Explained

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.