deft Posted March 25, 2006 Share Posted March 25, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5812-what-operators-to-use/ Share on other sites More sharing options...
toplay Posted March 25, 2006 Share Posted March 25, 2006 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 wantfor ($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.37else echo 'Rate not determined.';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5812-what-operators-to-use/#findComment-20721 Share on other sites More sharing options...
Barand Posted March 26, 2006 Share Posted March 26, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/5812-what-operators-to-use/#findComment-20814 Share on other sites More sharing options...
toplay Posted March 26, 2006 Share Posted March 26, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5812-what-operators-to-use/#findComment-20836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.