PHP, Complicated if statements?

  • Thread starter Thread starter evachick156
  • Start date Start date
E

evachick156

Guest
Im having a bit of trouble. I do calculations from $tot1 - $tot9...and i have if statements checking which is the smallest value.
if($tot1 < $tot2)
{
$min = $tot1;
}
else
{
$min = $tot2;
}
if($min > $tot3)
{
$min = $tot3;
}
if($min > $tot4)
{
$min = $tot4;
}
if($min > $tot5)
{
$min = $tot5;
}
if($min > $tot6)
{
$min = $tot6;
}
if($min > $tot7)
{
$min = $tot7;
}
if($min > $tot8)
{
$min = $tot8;
}
if($min > $tot9)
{
$min = $tot9;
}

Now my problem is sometimes the $tot1 - $tot9 can contain 0 values, which I dont want the min to be. What is the best way to check for the smallest value and make sure the $tot1 - $tot9 does not contain 0 for checking the minimum?
 
I'd put them in an array:

$tots = array();
$tots[] = $tot1;
$tots[] = $tot2;
$tots[] = $tot3;
$tots[] = $tot4;
$tots[] = $tot5;
$tots[] = $tot6;
$tots[] = $tot7;
$tots[] = $tot8;
$tots[] = $tot9;

Then sort the array.

sort($tots);

Then loop through it and return the first one that's not 0:

for ($i = 0; $i < sizeof($tots); $i++) {
if ($tots[$i] != 0) {
$min = $tots[$i];
break;
}
}

I tried this and it works!
 
Back
Top