> But then, if the user entered 5 - 6, it should be -1 but it'd return
> positive one... Is there another way?

Come on, man... this is addition and subtraction. You can't figure it out?

> > You simply need the absolute value of the difference.  So taking
Stephen's
> > example below..
> >
> > $total = 0;

Look, right here, you're setting the total to zero, so now everything will
be subtracted from zero. If you want everything to be subtracted from the
first number, the set $total equal to the first number and loop through the
remainder.

$total = $_POST['nums'][0];

$cnt = count($_POST['nums']);
for($x=1;$x<$cnt;$x++)
{ $total -= $_POST['nums'][$x]; }

And I'm sure your next post will be "but how will I use that if I want to go
back to addition/multiplication/division???";

Here, assuming $mode can be set to "add","sub","mul" or "div":

$total = $_POST['nums'][0];
$cnt = count($_POST['nums']);
for($x=1;$x<$cnt;$x++)
{
  switch($mode)
  {
    case "add": $total += $_POST['nums'][$x]; break;
    case "sub": $total -= $_POST['nums'][$x]; break;
    case "mul": $total *= $_POST['nums'][$x]; break;
    case "div": $total /= $_POST['nums'][$x]; break;
  }
}
echo $total;

Plenty of other ways to do it, too, that may be more efficient. And I
couldn't honestly tell you if *= and /= work and I don't want to go see.

Hope that helps. Good luck.

---John Holmes...


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to