Skip to content

Core\Math::clamp

Core\Math::clamp(T $n, T $low, T $high): TCore\Math::clamp(
    T $n,
    T $low,
    T $high,
): T

Keeps a value between a lowest and a highest value. A value below the range becomes the lowest value, and a value above it becomes the highest. A value inside the range stays the same, and both ends count as inside.

It works for numbers, strings, bool and null, as long as all three values can be compared with each other. When the lowest value is above the highest, there is no range, and Core\Math::clamp throws a RuntimeError.

Use it when a number comes from outside your program and has to stay within limits: a page size a visitor asks for, a volume, a percentage.

The examples below show whole numbers kept in a range, then a range with no values in it, then a page size limited to what a server allows.

Parameters

  • $nTThe value to keep in the range. It can be a number, a string, a bool or null.
  • $lowTThe smallest value the result can be.
  • $highTThe largest value the result can be. It must not be below $low.

Return value

Returns T. $low when $n is below it, $high when $n is above it, and $n itself otherwise. Both ends are inside the range.

Errors

  • RuntimeErrorWhen $low is above $high. Also when two of the values cannot be compared: an object without compareTo, an array, or a number and a string.

Examples

Keeping a number in range

<?nvs
// Shows how `Core\Math::clamp` keeps a number between 0 and 10.
// A number above the range becomes the highest value. This prints 10.
echo Core\Math::clamp(15, 0, 10), "\n";
// A number below the range becomes the lowest value. This prints 0.
echo Core\Math::clamp(-3, 0, 10), "\n";
// A number inside the range stays the same. This prints 7.
echo Core\Math::clamp(7, 0, 10), "\n";
// The ends are inside the range too. This prints 10.
echo Core\Math::clamp(10, 0, 10), "\n";
Output
10
0
7
10

A range with no values

<?nvs
// Shows ranges of decimal numbers, and a range that has no values in it.
// Here the range is from 0.5 to 1.0, so this prints 1.
echo Core\Math::clamp(1.25, 0.5, 1.0), "\n";
// The lowest value and the highest value can be the same. This prints 0.5.
echo Core\Math::clamp(1.25, 0.5, 0.5), "\n";
// A lowest value above the highest is not a range. The call throws a `RuntimeError`.
try {
echo Core\Math::clamp(1.25, 1.0, 0.5), "\n";
} catch (RuntimeError $e) {
echo 'error: ', $e->message, "\n";
}
Output
1
0.5
error: Core\Math::clamp was given a `low` above its `high`, which is an empty range

A page size limit

<?nvs
// Shows how a server limits the number of results a visitor asks for on one page.
class PageSize
{
// Every page shows at least 1 result and at most 100.
public static function allowed(int $asked): int
{
return Core\Math::clamp($asked, 1, 100);
}
}
// A visitor asks for 25 results. That is allowed, so this prints 25.
echo PageSize::allowed(25), "\n";
// A visitor asks for 5000 results. This prints 100.
echo PageSize::allowed(5000), "\n";
// A visitor asks for 0 results. This prints 1.
echo PageSize::allowed(0), "\n";
Output
25
100
1