15

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.

vsminkov
  • 10,912
  • 2
  • 38
  • 50
ILikeTacos
  • 17,464
  • 20
  • 58
  • 88
  • 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. – Wrikken Jul 15 '13 at 21:54
  • 1
    To "clamp" a number means to restrict it to a maximum (or minimum) value. That is not what you are asking for here. – Alnitak Jul 15 '13 at 22:03
  • That's why I quoted clamp, for lack of a better word. – ILikeTacos Jul 15 '13 at 22:08
  • 2
    It's a sanitizer. It restricts a value to any of the predefined values, and replaces any violations with the default value. – Sven Jul 15 '13 at 22:11

4 Answers4

67

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));
}
PhotonFalcon
  • 894
  • 8
  • 10
8
$value = in_array($value, $possible_values, true) ? $value : $default_value;

Ref: https://php.net/in_array

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 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_contents – ILikeTacos Jul 15 '13 at 21:53
  • 2
    I just rollbacked because the second returned `true` or `$default_value`. Leave your post correct when it's correct ;-) – bwoebi Jul 15 '13 at 21:54
  • @AlanChavez: No such built-in function exists. – Alix Axel Jul 15 '13 at 22:14
  • I'm marking this question as correct, even though I wasn't asking for the code :) – ILikeTacos Jul 15 '13 at 22:26
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)

hakre
  • 193,403
  • 52
  • 435
  • 836
jcmargentina
  • 181
  • 1
  • 6
0

[$min,$max], $value:

$result = ($value >= $min && $value <= $max) ? $value: ($value < $min ? $min : $max)