PHP range Function
last modified March 13, 2025
The PHP range
function creates an array containing a range of
elements. It's useful for generating sequences of numbers or characters.
Basic Definition
The range
function generates an array with elements from a start
value to an end value. It can create sequences of numbers or characters.
Syntax: range(mixed $start, mixed $end, int|float $step = 1): array
.
The step parameter controls the increment between elements in the sequence.
Basic Numeric Range Example
This example demonstrates creating a simple numeric range from 1 to 5.
<?php $numbers = range(1, 5); print_r($numbers);
This creates an array with numbers from 1 to 5. The default step of 1 is used when not specified. The array includes both start and end values.
Range With Custom Step
This shows how to create a range with a custom increment between values.
<?php $evenNumbers = range(0, 10, 2); print_r($evenNumbers);
This generates even numbers from 0 to 10. The step parameter of 2 creates each subsequent number by adding 2 to the previous value.
Descending Range
The range function can also create sequences in descending order.
<?php $countdown = range(5, 1); print_r($countdown);
When the start value is greater than the end value, range creates a descending sequence. The default step of -1 is used automatically.
Character Range
The range function works with characters, creating alphabetical sequences.
<?php $letters = range('a', 'e'); print_r($letters);
This generates an array of lowercase letters from 'a' to 'e'. Character ranges follow the ASCII/Unicode sequence for the given characters.
Floating Point Range
Range can work with floating point numbers, though precision should be noted.
<?php $decimalRange = range(0.1, 0.5, 0.1); print_r($decimalRange);
This creates a range of decimal numbers. Be cautious with floating-point precision as rounding errors might occur in some cases.
Best Practices
- Memory Usage: Large ranges consume significant memory.
- Step Values: Ensure step direction matches range direction.
- Character Ranges: Works with single-byte characters.
- Performance: Consider alternatives for very large ranges.
Source
This tutorial covered the PHP range
function with practical
examples showing its usage for creating numeric and character sequences.
Author
List all PHP Array Functions.