Skip to content
On this page

Expressions: Range Operations

A range operation is a way to create a range of integer values. There are six different types of range operations: from, to, to inclusive, between, between inclusive, and full.

The range operation is way of constructing a Psl\Range\RangeInterface object, according to the following table:

SyntaxTypeRange
start..Psl\Range\FromRangestart ≤ x
..endPsl\Range\ToRangex < end
..=endPsl\Range\ToRangex ≤ end
start..endPsl\Range\BetweenRangestart ≤ x < end
start..=endPsl\Range\BetweenRangestart ≤ x ≤ end
..Psl\Range\FullRange-

The following operations are equivalent:

$a = new Psl\Range\between(1, 10, false);
$b = 1..10;

Psl\invariant($a == $b);

Ranges can be iterated over as long as the range has a starting point:

foreach 1..100 as $value {
    // $value is 1, 2, 3, ..., 99
}

From

A from range operation can be used to create a range from a starting point to infinity. This is done using the .. operator following the starting point.

$a = 1..;

To

A to range operation can be used to create a range to an ending point. This is done using the .. operator preceding the ending point.

$a = ..10;

To Inclusive

A to inclusive range operation can be used to create a range to an ending point, inclusive. This is done using the ..= operator preceding the ending point.

$a = ..=10;

Between

A between range operation can be used to create a range from a starting point to an ending point. This is done using the .. operator between the starting and ending points.

$a = 1..10;

Between Inclusive

A between inclusive range operation can be used to create a range from a starting point to an ending point, inclusive. This is done using the ..= operator between the starting and ending points.

$a = 1..=10;

Full

A full range operation can be used to create a range to infinity. This is done using the .. operator.

$a = ..;

Licensed under either of the MIT License or the Apache License (Version 2.0), at your option.