clamp() v2: A Small RFC, But a Big Step for PHP?

A new RFC proposes adding clamp() as a native function in PHP. This function would constrain a value within a defined range — just like in many modern languages (JavaScript, Kotlin, Rust…).

clamp($value, $min, $max); // returns $min, $max or $value depending on the case

Rejected once back in 2020, the idea is making a comeback in a more refined version:

  • it throws an error if $min > $max,
  • has clear behavior,
  • and uses strict comparison.

The RFC is still under discussion, but this time, it seems to be receiving a warmer welcome. If adopted, this small function could quickly become a new go-to in our projects.

And if you’d like to know more about what’s coming in PHP, check out our article on the upcoming 8.5 release!

php