note 86963 added to function.min
| From: | mlester at ndsuk dot com | Date: | Wed, 12 Nov 2008 15:59:58 +0000 |
| Subject: | note 86963 added to function.min | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146446@lists.php.net to get a copy of this message | ||
I have found a very useful trick to help get round the problem of setting a variable to max int when
finding a min i.e.
$val = 10;
$min = 100000; // This is unpleasant and I couldn't find a equivalent to the C++ MAXINT
if ($val < $min)
{
$min = $val;
}
try this ...
$dataSet = Array(7, 8, 9, -1, -100, 0, 1, 2, 3, 5, 6);
$min = true;
$max = true;
foreach ($dataSet as $data )
{
echo("data = $data");
$min = min($data, $min);
$max = max($data, $max);
}
echo("min = $min");
echo("max = $max");
$max can be set to anything e.g. "infinity", but the same trick doesn't work with
min, however true does work (false doesn't). Not sure why though.
The above code even works with a data set like this...
$dataSet = Array("0.5", 1, 2, "3", "-1", "5");
but min doesn't like null or negative float e.g. "-1.2" and "" can give
some odd results too.
----
Server IP: 92.48.74.199
Probable Submitter: 194.202.59.151
----
Manual Page -- http://www.php.net/manual/en/function.min.php
Edit -- https://master.php.net/note/edit/86963
Del: integrated -- https://master.php.net/note/delete/86963/integrated
Del: useless -- https://master.php.net/note/delete/86963/useless
Del: bad code -- https://master.php.net/note/delete/86963/bad+code
Del: spam -- https://master.php.net/note/delete/86963/spam
Del: non-english -- https://master.php.net/note/delete/86963/non-english
Del: in docs -- https://master.php.net/note/delete/86963/in+docs
Del: other reasons-- https://master.php.net/note/delete/86963
Reject -- https://master.php.net/note/reject/86963
Search -- https://master.php.net/manage/user-notes.php