Does a "clamp" number function exist in PHP?
$value = in_array($value, $possible_values) ? $value : $default_value;
It seems as though you are just trying to find a number within a set. An actual clamp function will make sure a number is within 2 numbers (a lower bounds and upper bounds). So psudo code would be clamp(55, 1, 10)
would produce 10
and clamp(-15, 1, 10)
would produce 1
where clamp(7, 1, 10)
would produce 7
. I know you are looking for more of an in_array
method but for those who get here from Google, here is how you can clamp in PHP without making a function (or by making this into a function).
max($min, min($max, $current))
For example:
$min = 1;
$max = 10;
$current = 55;
$clamped = max($min, min($max, $current));
// $clamped is now == 10
A simple clamp method would be:
function clamp($current, $min, $max) {
return max($min, min($max, $current));
}
I think is worth to know that...
https://wiki.php.net/rfc/clamp
Is approved and will exist in the core