Does a "clamp" number function exist in PHP?
Asked Answered
T

4

16

I wrote a function to "clamp" numbers in PHP, but I wonder if this function exists natively in the language.

I read PHP.net documentation in the math section, but I couldn't find it.

Basically what my function does is that it accepts a variable, an array of possible values, and a default value, this is my function's signature:

function clamp_number($value, $possible_values, $default_value)

If $value does not match any of the $possible_values then it defaults to $default_value

I think my function would be way faster if PHP already provides it natively because I'm using quite often in my program.

Tequilater answered 15/7, 2013 at 21:49 Comment(4)
Unless it's a defined integer range (in which case you could use filter_var), nope, no built in. To determine wether we can speed up your custom function we'd need to see it.Merilynmeringue
To "clamp" a number means to restrict it to a maximum (or minimum) value. That is not what you are asking for here.Imputable
That's why I quoted clamp, for lack of a better word.Tequilater
It's a sanitizer. It restricts a value to any of the predefined values, and replaces any violations with the default value.Compliment
P
8
$value = in_array($value, $possible_values, true) ? $value : $default_value;

Ref: https://php.net/in_array

Plutonium answered 15/7, 2013 at 21:51 Comment(4)
That's what I'm currently doing to see if the value falls within the possible options, I just thought that PHP could already have it as part of the language, probably like an alias... similar to put_file_contentsTequilater
I just rollbacked because the second returned true or $default_value. Leave your post correct when it's correct ;-)Sapphire
@AlanChavez: No such built-in function exists.Coleman
I'm marking this question as correct, even though I wasn't asking for the code :)Tequilater
G
68

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));
}
Gigantes answered 16/2, 2016 at 17:11 Comment(0)
P
8
$value = in_array($value, $possible_values, true) ? $value : $default_value;

Ref: https://php.net/in_array

Plutonium answered 15/7, 2013 at 21:51 Comment(4)
That's what I'm currently doing to see if the value falls within the possible options, I just thought that PHP could already have it as part of the language, probably like an alias... similar to put_file_contentsTequilater
I just rollbacked because the second returned true or $default_value. Leave your post correct when it's correct ;-)Sapphire
@AlanChavez: No such built-in function exists.Coleman
I'm marking this question as correct, even though I wasn't asking for the code :)Tequilater
S
4

I think is worth to know that a PHP clamp() function was proposed for version PHP 8.2:

https://wiki.php.net/rfc/clamp

If it would have been approved, it would exist in the core.

(The clamp() request for comments (RFC) has been withdrawn.)

I have made the difficult decision to withdraw the RFC, I currently don't have the resources or bandwidth for it. If anyone would like to take it over they are more than welcome to, I apologize to anyone who was looking forward to this. 🙏 (Kim Hallberg "thinkverse"; Jun 2023; ref)

Serenata answered 23/7, 2022 at 22:48 Comment(0)
M
0

[$min,$max], $value:

$result = ($value >= $min && $value <= $max) ? $value: ($value < $min ? $min : $max)
Motile answered 4/7, 2023 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.