Jump to content


Photo

what operators to use


  • Please log in to reply
3 replies to this topic

#1 deft

deft
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 25 March 2006 - 10:45 PM

anybody care to help me simplify this?

if ($fromzip == 98101 && $tozip == 98004 || $fromzip == 98004 && $tozip == 98101)
$baserate = 18.37;
elseif ($fromzip == 98101 && $tozip == 98005 || $fromzip == 98005 && $tozip == 98101)
$baserate = 19.37;


I'm trying to say "if either the from-zip is X and the to-zip is Y or vice-versa" in such a way that I don't have to write them both twice.




#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 25 March 2006 - 11:21 PM

What you have is fine. You'll need two iterations since you're not using ranges. So, either your way, or this way:

if (($fromzip == 98004 || $fromzip == 98101) && ($tozip == 98004 || $tozip == 98101)) { }

You can always, use an array to hold your base rate and zip codes so you won't have to keep adding 'if' statements every time you need to add a new base rate. See example code below. You can always have this data in an external file and have it be read too. The example code assumes you're not dealing with zip code ranges but actual specific values that must match. Otherwise a greater than or equal to (>=) and less than or equal to (<=) approach would be better.

<?PHP
$fromzip = 98004;  // Just an example
$tozip = 98101;    // Just an example

$rates = array(array('rate' => 18.37, 'zip1' => 98004, 'zip2' => 98101),
               array('rate' => 19.37, 'zip1' => 98005, 'zip2' => 98101)
              );

$rate = 0;  // Set rate to whatever default you want

for ($i=0,$cnt=count($rates); $i < $cnt; $i++) {
    if (($fromzip == $rates[$i]['zip1'] || $fromzip == $rates[$i]['zip2']) &&
        ($tozip   == $rates[$i]['zip1'] || $tozip   == $rates[$i]['zip2'])) {
        $rate = $rates[$i]['rate'];
        break;
    }
}

if ($rate > 0)
    echo 'Rate: ', $rate;   // displays: Rate: 18.37
else
    echo 'Rate not determined.';
?>


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 March 2006 - 11:44 AM

You could simplify the IF() in the above by using

if (in_array($fromzip,$rates[$i]) && in_array($tozip,$rates[$i]))

in place of

if (($fromzip == $rates[$i]['zip1'] || $fromzip == $rates[$i]['zip2']) &&
        ($tozip   == $rates[$i]['zip1'] || $tozip   == $rates[$i]['zip2']))

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 02:38 PM

Yes, of course. However, I feel that in_array() would be slower to use in this case since there's only two values that you want to evaluate. If there were lots of values, I would use it. Also, you force the in_array() to evaluate the base rate needlessly too.

Deft, do it any way you want.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users