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

From: Date: Wed, 17 Jun 2015 13:16:56 +0000
Subject: Req #69862 [Opn]: PHP function to clamp numbers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-193608@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
 User updated by:    david dot proweb at gmail dot com
 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:

@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.


Previous Comments:
------------------------------------------------------------------------
[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


Thread (6 messages)

« previous php.bugs (#193608) next »