Req #69862 [Com]: PHP function to clamp numbers

From: Date: Fri, 05 May 2017 10:38:53 +0000
Subject: Req #69862 [Com]: PHP function to clamp numbers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-208960@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69862&edit=1 ID: 69862 Comment by: devsmt at gmail dot com Reported by: david dot proweb at gmail dot com Summary: PHP function to clamp numbers Status: Closed Type: Feature/Change Request Package: Math related Operating System: any PHP Version: 7.0.0alpha1 Assigned To: kalle Block user comment: N Private report: N New Comment: the problem in composer/namespaces era is people rewrite this function(and similar) many times as it is needed for param validation and call it with different names, leading to duplication, waste and fatigue, although small. Previous Comments: ------------------------------------------------------------------------ [2015-06-18 10:23:31] kalle@php.net 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. ------------------------------------------------------------------------ [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

« previous php.bugs (#208960) next »