Jump to content

what operators to use


deft

Recommended Posts

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.


Link to comment
Share on other sites

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.

[code]
<?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.';
?>
[/code]
Link to comment
Share on other sites

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

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

in place of

[code]if (($fromzip == $rates[$i]['zip1'] || $fromzip == $rates[$i]['zip2']) &&
        ($tozip   == $rates[$i]['zip1'] || $tozip   == $rates[$i]['zip2']))[/code]
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.