Req #69862 [NEW]: PHP function to clamp numbers
From: david dot proweb at gmail dot com
Operating system: any
PHP version: 7.0.0alpha1
Package: Math related
Bug Type: Feature/Change Request
Bug description:PHP function to clamp numbers
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 bug report at https://bugs.php.net/bug.php?id=69862&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69862&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69862&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69862&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69862&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69862&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69862&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69862&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69862&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69862&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69862&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69862&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69862&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69862&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69862&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69862&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69862&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69862&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69862&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69862&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69862&r=mysqlcfg
Thread (6 messages)
- david dot proweb at gmail dot com