|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2015-06-17 13:11 UTC] laruence@php.net
[2015-06-17 13:16 UTC] david dot proweb at gmail dot com
[2015-06-17 14:05 UTC] laruence@php.net
[2015-06-18 10:23 UTC] kalle@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: kalle
[2015-06-18 10:23 UTC] kalle@php.net
[2017-05-05 10:38 UTC] devsmt at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 23 08:00:02 2025 UTC |
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)