Edit report at https://bugs.php.net/bug.php?id=69862&edit=1
ID: 69862
Updated by: kalle@php.net
Reported by: david dot proweb at gmail dot com
Summary: PHP function to clamp numbers
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Math related
Operating System: any
PHP Version: 7.0.0alpha1
-Assigned To:
+Assigned To: kalle
Block user comment: N
Private report: N
New Comment:
I agree with Xinchen here, some functions even are simply better off being implemented in native
PHP. Even some functions like atanh(), rad2deg() and so on could probably have been better of being
implemented in PHP.
My best suggestion here is to make a PR and post it to the internals for some debate.
Previous Comments:
------------------------------------------------------------------------
[2015-06-17 14:05:28] laruence@php.net
to be honest, "we should do it because we can, and we did similar things" is a annoying
argument.
anyway, the reason why I'd like don't add this is because, I have never try to use such
function before..
this is just my 2cents.
------------------------------------------------------------------------
[2015-06-17 13:16:56] david dot proweb at gmail dot com
@laruence there a lot PHP functions that we can implement with few lines, but PHP supports it
natively. This function will help you make this natively and avoid users recreate it.
------------------------------------------------------------------------
[2015-06-17 13:11:54] laruence@php.net
I don't understand why you need this builtin.
as you can implement very easily buy just 3 lines codes
------------------------------------------------------------------------
[2015-06-17 12:32:29] david dot proweb at gmail dot com
Description:
------------
Need a PHP function to clamp numbers, natively. Currently it is possible, but will be great if it
supports natively.
function clamp($number, $minValue, $maxValue) {
return max($minValue, min($maxValue, $number));
}
Basically, it will return the $number if is greater than $minValue and lower than $maxValue. If
$number is lower than $minValue, it returns $minValue. If $number is greater than $maxValue, it
returns $maxValue.
clamp(2, 1, 3); // => 2 (in range)
clamp(1, 1, 3); // => 1 (in range, min value)
clamp(3, 1, 3); // => 3 (in range, max value)
clamp(0, 1, 3); // => 1 (out range, using min value)
clamp(4, 1, 3); // => 4 (out range, using max value)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69862&edit=1