range
range(string|int|float $start, string|int|float $end, int|float $step = 1): array
Range arrays: eagerly builds an arithmetic array from start/end/step — integers auto-detect direction (no negative step needed), characters have builtin sequences (range('a','e')), float steps are allowed; the end is included in the result (opposite of Python's exclusive stop).
C C23
#include <stdio.h>
/* no range builder: hand-rolled loops into a pre-sized array */
static void print_ints(const int *v, int n) {
printf("[");
for (int i = 0; i < n; i++) {
printf("%d%s", v[i], i + 1 < n ? ", " : "");
}
printf("]\n");
}
int main(void) {
int odd[5], down[5], neg[4];
for (int i = 0; i < 5; i++) { odd[i] = 2 * i + 1; }
for (int i = 0; i < 5; i++) { down[i] = 5 - i; }
for (int i = 0; i < 4; i++) { neg[i] = 10 - 3 * i; }
print_ints(odd, 5);
print_ints(down, 5);
char letters[6];
for (int i = 0; i < 5; i++) { letters[i] = (char)('a' + i); }
letters[5] = '\0';
printf("%s\n", letters);
print_ints(neg, 4);
return 0;
}
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
C++ C++26
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
// C++20 ranges: views::iota builds ascending; no step argument - compose filter/map
static std::string join(const std::vector<int> &v) {
std::string out = "[";
for (size_t i = 0; i < v.size(); i++) {
out += std::to_string(v[i]);
if (i + 1 < v.size()) { out += ", "; }
}
return out + "]";
}
int main() {
auto odd = std::views::iota(1, 11) | std::views::filter([](int i) { return i % 2 == 1; });
std::vector<int> oddv(odd.begin(), odd.end());
std::cout << join(oddv) << "\n";
auto down = std::views::iota(1, 6) | std::views::reverse;
std::vector<int> downv(down.begin(), down.end());
std::cout << join(downv) << "\n";
std::string letters;
for (int c : std::views::iota(0, 5)) { letters += static_cast<char>('a' + c); }
std::cout << letters << "\n";
auto neg = std::views::iota(0, 4) | std::views::transform([](int i) { return 10 - 3 * i; });
std::vector<int> negv(neg.begin(), neg.end());
std::cout << join(negv) << "\n";
return 0;
}
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
C# v11.0.100
using System;
using System.Linq;
using System.Text;
// Enumerable.Range(start, count): always ascending; stepping has to Select an arithmetic map
Console.WriteLine("[" + string.Join(", ", Enumerable.Range(1, 5).Select(i => 2 * i - 1)) + "]");
Console.WriteLine("[" + string.Join(", ", Enumerable.Range(1, 5).Select(i => 6 - i)) + "]");
var chars = new StringBuilder();
foreach (var c in Enumerable.Range(0, 5).Select(i => (char)('a' + i))) chars.Append(c);
Console.WriteLine(chars);
Console.WriteLine("[" + string.Join(", ", Enumerable.Range(0, 4).Select(i => 10 - 3 * i)) + "]");
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
Go v1.27.4
package main
import (
"fmt"
"strings"
)
// no range-builder: hand-rolled loops (Go's `range` keyword iterates, it does not build)
func joinInts(v []int) string {
parts := make([]string, len(v))
for i, x := range v {
parts[i] = fmt.Sprintf("%d", x)
}
return "[" + strings.Join(parts, ", ") + "]"
}
func main() {
var odd []int
for i := 1; i <= 10; i += 2 {
odd = append(odd, i)
}
fmt.Println(joinInts(odd))
var down []int
for i := 5; i >= 1; i-- {
down = append(down, i)
}
fmt.Println(joinInts(down))
letters := []rune{}
for c := 'a'; c <= 'e'; c++ {
letters = append(letters, c)
}
fmt.Println(string(letters))
var neg []int
for i := 10; i >= 1; i -= 3 {
neg = append(neg, i)
}
fmt.Println(joinInts(neg))
}
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
Java v27.0.1
import java.util.List;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
// IntStream: ranges need rangeClosed; stepping has to filter or map (no step argument)
List<Integer> odd = IntStream.rangeClosed(1, 10).filter(i -> i % 2 == 1).boxed().toList();
System.out.println(odd);
List<Integer> down = IntStream.rangeClosed(1, 5).map(i -> 6 - i).boxed().toList();
System.out.println(down);
StringBuilder chars = new StringBuilder();
for (int c = 'a'; c <= 'e'; c++) chars.append((char) c);
System.out.println(chars);
List<Integer> neg = IntStream.iterate(10, i -> i - 3).limit(4).boxed().toList();
System.out.println(neg);
}
}
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
JavaScript v26.13.1
// no built-in builder: Array.from with a mapping function composes one
const build = (n, f) => Array.from({ length: n }, (_, i) => f(i));
console.log(`[${build(5, (i) => 2 * i + 1).join(", ")}]`);
console.log(`[${build(5, (i) => 5 - i).join(", ")}]`);
console.log(build(5, (i) => String.fromCharCode(97 + i)).join(""));
console.log(`[${build(4, (i) => 10 - 3 * i).join(", ")}]`); // negative step: hand-rolled
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
PHP v8.6.0
// integers with a step: 1,3,5,7,9
echo "[", implode(", ", range(1, 10, 2)), "]\n";
// descending: NO negative step needed (direction auto-detected)
echo "[", implode(", ", range(5, 1)), "]\n";
// characters: built-in sequence support
echo implode("", range("a", "e")), "\n";
// explicit negative step (the end is INCLUSIVE, unlike Python's exclusive stop)
echo "[", implode(", ", range(10, 0, -3)), "]\n";
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
Python v3.15.2
# range: native lazy sequence - PHP's range() builds the whole array eagerly
print(list(range(1, 10, 2))) # [1, 3, 5, 7, 9]
print(list(range(5, 0, -1))) # descending needs an explicit NEGATIVE step
print("".join(chr(c) for c in range(97, 102))) # "abcde": chars via ord/chr composition
print(list(range(10, -1, -3))) # [10, 7, 4, 1]: STOP is exclusive (PHP end is inclusive!)
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
Rust v1.99.1
fn main() {
// native iterators: step_by / rev are the closest std match to PHP's range
let odd: Vec<i32> = (1..=10).step_by(2).collect();
println!("{:?}", odd);
let down: Vec<i32> = (1..=5).rev().collect();
println!("{:?}", down);
let chars: String = (b'a'..=b'e').map(char::from).collect();
println!("{}", chars);
let neg: Vec<i32> = (0..4).map(|i| 10 - 3 * i).collect();
println!("{:?}", neg);
}
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
TypeScript v26.13.1
// no built-in builder: Array.from with a mapping function composes one
const build = (n: number, f: (i: number) => number | string): (number | string)[] =>
Array.from({ length: n }, (_, i) => f(i));
console.log(`[${build(5, (i) => 2 * i + 1).join(", ")}]`);
console.log(`[${build(5, (i) => 5 - i).join(", ")}]`);
console.log(build(5, (i) => String.fromCharCode(97 + i)).join(""));
console.log(`[${build(4, (i) => 10 - 3 * i).join(", ")}]`); // negative step: hand-rolled
[1, 3, 5, 7, 9] [5, 4, 3, 2, 1] abcde [10, 7, 4, 1]
Language differences
Contrast with array_fill (existing entry): array_fill(index, count, value) fills one constant, range(start, end, step) generates a progression — "N identical values call for fill, N differing values call for range".
Boundary semantics are the biggest teaching point: PHP's end is inclusive (range(1,10,2) reaches 9; range(10,0,-3) includes 1), while Python's stop is exclusive (range(1,10,2) is likewise 1..9 but only range(10,-1,-3) gives 10,7,4,1); PHP auto-detects descending direction (range(5,1) needs no negative step) while Python demands an explicit negative step; character sequences are builtin in PHP (range("a","e") just works) and everywhere else composed via ord/chr — exactly the composition the existing chr_ord entry demonstrates.
Lazy vs eager: PHP's range() builds the whole array immediately (big intervals eat memory); Python's range() is a lazy object (O(1) len, no interval-sized memory until list() materializes it); Java IntStream, C# Enumerable and Rust iterators are likewise lazy or half-lazy.
Per-language surfaces: Python is native but rejects floats (range(0.0,1.0,0.5) is an immediate TypeError — PHP allows float steps); Java's IntStream.rangeClosed has no step parameter (filter odds or map an arithmetic function; iterate expresses arbitrary stepping); C#'s Enumerable.Range is ascending-only (a count, not an end); JS/TS Array.from({length}, map) is the community idiom; Go's range keyword is iteration sugar over existing collections with entirely different semantics — building requires hand loops; C/C++ hand loops (C++20 views::iota expresses ascending; views::stride needs C++23); Rust's (1..=10).step_by(2) is the closest std match.