note 86963 modified in function.min by danbrown
| From: | danbrown@php.net | Date: | Wed, 12 Nov 2008 16:11:01 +0000 |
| Subject: | note 86963 modified in function.min by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146449@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.
<?php
$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 ...
<?php
$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...
<?php $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.
--was--
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.
http://php.net/manual/en/function.min.php