The new clamp() function in PHP 8.6
You know how sometimes you want to ensure that a value stays within a specific range? Maybe you’re working with user input, configuration values, or any scenario where you need to enforce boundaries.
In scenarios like these, having a built-in function to clamp values can be incredibly useful. Well, good news! PHP 8.6 will be introducing a new function called clamp() that does exactly that.
What is the clamp() function?
PHP 8.6’s clamp() function allows you to restrict a value to be within a specified minimum and maximum range. If the value is less than the minimum, it returns the minimum; if it’s greater than the maximum, it returns the maximum; otherwise, it returns the value itself.
Here’s the signature of the function:
clamp ( mixed $value, mixed $min, mixed $max ) : mixed
As you can tell, clamp takes three arguments, a $value, $min, and $max, then checks if $value is within the bounds of $min and $max (both inclusive).
- If
$valueis less than$min, it returns$min. - If
$valueis greater than$max, it returns$max. - If
$valueis between$minand$max, it returns$value.
It throws a ValueError if min > max or if min/max are NAN.
Here’s a simple example of how it works:
$value1 = clamp(15, 10, 20); // Returns 15
$value2 = clamp(5, 10, 20); // Returns 10
$value3 = clamp(25, 10, 20); // Returns 20
Clamp with named parameters
The clamp function becomes even more straight-forward with named parameters and re-ordering of the arguments.
$brightness = clamp(min: 0, value: $brightness, max: 100);
Real-world use cases
Here are some practical scenarios where the clamp() function can come in handy.
User input: keep a percentage between 0 and 100
$percentage = clamp($percentage, 0, 100);
UI sliders: constrain a volume setting between 0 and 10
$volume = clamp($volume, 0, 10);
Pagination: bound page number between the first and last page
$page = clamp((int)$_GET['page'] ?? 1, 1, $totalPages);
Rate limiting: prevent burst count from exceeding a ceiling
$requests = clamp($requests, 0, $maxBurst);
Dates: ensure a booking date falls within an allowed window
$date = new DateTimeImmutable($input);
$start = new DateTimeImmutable('2025-08-15');
$end = new DateTimeImmutable('2025-09-15');
$clamped = clamp($date, $start, $end); // returns start/end/date accordingly
Geometry: restrict an angle between 0 and 90
$angle = clamp($angle, 0, 90);
Strings (lexicographic): keep a tag between “c” and “g”
$tag = clamp($tag, "c", "g");
Conclusion
The clamp() function in PHP 8.6 is a simple yet powerful addition that can help you manage values effectively by enforcing boundaries. Whether you’re dealing with user input, configuration settings, or any scenario where you need to ensure values stay within a specific range, clamp() provides a clean and efficient solution.
Read more about the clamp() function in this RFC.
👋 Hi there! This is Amit, again. I write articles about all things web development. If you enjoy my work (the articles, the open-source projects, my general demeanour... anything really), consider leaving a tip & supporting the site. Your support is incredibly appreciated!