clamp
Return the given value if in range, else return the nearest bound
Description
mixed clamp(mixed $value, mixed $min, mixed $max)
Return the given value if in range of min and max.
Else if it's lower than min, return min.
Else return max.
Note:
Values of different types will be compared using the
standard comparison rules. For instance, when a non-numeric string is
compared to an int, the int is cast to string and the
values are compared as strings. Multiple non-numeric string values are also compared as
strings. The actual value returned will be of the original type with no conversion applied.
Caution
Be careful when passing arguments of different types because
clamp can produce unpredictable results.
Parameters
-
value
-
Any comparable
value to be clamped between
min and max.
-
min
-
A comparable minimum to limit
the
value to.
-
max
-
A comparable maximum to limit
the
value to.
Return Values
clamp returns the parameter value if
considered "between" min and max according to standard
comparisons.
If value is NAN, then the returned value will also be
NAN.
Errors/Exceptions
If min is greater than max,
clamp throws a ValueError.
If min or max is NAN,
clamp throws a ValueError.