PHP中是否存在“clamp”数字函数?

时间:2022-11-23 09:13:01

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

我在PHP中编写了一个函数来“箝制”数字,但我想知道这个函数是否在语言中存在。

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

我在数学部分阅读了php。net文档,但是我找不到。

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)

函数clamp_number(价值,possible_values美元,美元default_value)

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

如果$value不匹配任何$possible_values,则默认为$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.

我认为如果PHP已经提供了它,我的函数会更快,因为我在程序中经常使用它。

Anyways if this question does not belong to SO, feel free to vote to close it.

无论如何,如果这个问题不属于这样,请投票结束它。

3 个解决方案

#1


5  

$value = in_array($value, $possible_values) ? $value : $default_value;

#2


17  

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).

似乎你只是想在一个集合中找到一个数字,一个实际的箝位函数将确保一个数字在两个数字内(下界和上界)。所以psudo代码是clamp(551,10)会生成10,clamp(- 15,1,10)会生成1,而clamp(7,1,10)会生成7。我知道您正在寻找更多的in_array方法,但是对于那些从谷歌获得这个方法的人来说,这里介绍了如何在不创建函数(或将其转换为函数)的情况下对PHP进行箝入。

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));
}

#3


0  

This is a good question, but to the best of my knowledge no. It would be much better (as well as easy) to implemet this in C as part of a standard PHP library, but no. You must do it in userspace.

这是个好问题,但据我所知不是。作为标准PHP库的一部分,在C中实现这一点会更好(也更容易),但不会。必须在用户空间中执行。

#1


5  

$value = in_array($value, $possible_values) ? $value : $default_value;

#2


17  

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).

似乎你只是想在一个集合中找到一个数字,一个实际的箝位函数将确保一个数字在两个数字内(下界和上界)。所以psudo代码是clamp(551,10)会生成10,clamp(- 15,1,10)会生成1,而clamp(7,1,10)会生成7。我知道您正在寻找更多的in_array方法,但是对于那些从谷歌获得这个方法的人来说,这里介绍了如何在不创建函数(或将其转换为函数)的情况下对PHP进行箝入。

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));
}

#3


0  

This is a good question, but to the best of my knowledge no. It would be much better (as well as easy) to implemet this in C as part of a standard PHP library, but no. You must do it in userspace.

这是个好问题,但据我所知不是。作为标准PHP库的一部分,在C中实现这一点会更好(也更容易),但不会。必须在用户空间中执行。