clamp
clamp is a PHP package that provides the clamp() function — constraining a value to a given range.
info
Starting with PHP 8.6, a native clamp() function will be available in PHP (see the PHP RFC: clamp() function v2). This package's implementation matches that behaviour, so migrating later is a drop-in change.
What it does
clamp($value, $min, $max) returns $value if it lies between $min and $max, otherwise it returns the nearer bound.
clamp(2, 1, 10); // 2
clamp(0, 1, 10); // 1 — below min
clamp(15, 1, 10); // 10 — above max
The package also provides clampMinMax(), which is equivalent but implemented differently and runs about 2× slower (see Performance).