note 81301 added to function.max
| From: | harmor at osu1 dot php dot net | Date: | Thu, 21 Feb 2008 17:56:09 +0000 |
| Subject: | note 81301 added to function.max | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136362@lists.php.net to get a copy of this message | ||
A way to bound a integer between two values is:
function bound($x, $min, $max)
{
return min(max($x, $min), $max);
}
which is the same as:
$tmp = $x;
if($tmp < $min)
{
$tmp = $min;
}
if($tmp > $max)
{
$tmp = $max;
}
$y = $tmp;
So if you wanted to bound an integer between 1 and 12 for example:
Input:
$x = 0;
echo bound(0, 1, 12).'<br />';
$x = 1;
echo bound($x, 1, 12).'<br />';
$x = 6;
echo bound($x, 1, 12).'<br />';
$x = 12;
echo bound($x, 1, 12).'<br />';
$x = 13;
echo bound($x, 1, 12).'<br />';
Output:
1
1
6
12
12
----
Server IP: 208.69.120.35
Probable Submitter: 66.27.49.42
----
Manual Page -- http://www.php.net/manual/en/function.max.php
Edit -- https://master.php.net/note/edit/81301
Del: integrated -- https://master.php.net/note/delete/81301/integrated
Del: useless -- https://master.php.net/note/delete/81301/useless
Del: bad code -- https://master.php.net/note/delete/81301/bad+code
Del: spam -- https://master.php.net/note/delete/81301/spam
Del: non-english -- https://master.php.net/note/delete/81301/non-english
Del: in docs -- https://master.php.net/note/delete/81301/in+docs
Del: other reasons-- https://master.php.net/note/delete/81301
Reject -- https://master.php.net/note/reject/81301
Search -- https://master.php.net/manage/user-notes.php