note 81302 added to function.min

From: Date: Thu, 21 Feb 2008 17:58:49 +0000
Subject: note 81302 added to function.min
Groups: php.notes 
Request: Send a blank email to php-notes+get-136363@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.min.php Edit -- https://master.php.net/note/edit/81302 Del: integrated -- https://master.php.net/note/delete/81302/integrated Del: useless -- https://master.php.net/note/delete/81302/useless Del: bad code -- https://master.php.net/note/delete/81302/bad+code Del: spam -- https://master.php.net/note/delete/81302/spam Del: non-english -- https://master.php.net/note/delete/81302/non-english Del: in docs -- https://master.php.net/note/delete/81302/in+docs Del: other reasons-- https://master.php.net/note/delete/81302 Reject -- https://master.php.net/note/reject/81302 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136363) next »