A proposal to add a native clamp() function to PHP that constrains a value within specified minimum and maximum bounds. The function returns the value if it's within range, or the nearest bound if outside. It supports mixed types (numbers, strings, DateTime objects) following PHP's comparison rules, throws ValueError for invalid bounds (min > max or NAN bounds), and offers performance improvements over userland implementations using min/max. The RFC is currently under discussion for inclusion in the next PHP 8.x release.
3 Comments
Sort: