Edit report at https://bugs.php.net/bug.php?id=69862&edit=1
ID: 69862
Updated by: laruence@php.net
Reported by: david dot proweb at gmail dot com
Summary: PHP function to clamp numbers
Status: Open
Type: Feature/Change Request
Package: Math related
Operating System: any
PHP Version: 7.0.0alpha1
Block user comment: N
Private report: N
New Comment:
I don't understand why you need this builtin.
as you can implement very easily buy just 3 lines codes
Previous Comments:
------------------------------------------------------------------------
[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