cursed Posted October 7, 2009 Share Posted October 7, 2009 Hello All, $country = file_get_contents("http://api.hostip.info/country.php?ip=$ip"); if ($country=="UK" || $country=="US" || $country=="AU") { //do something } I want to add 10-20 more ORs. Is there a better way to check these? Thanks in advance for the newbie question. Quote Link to comment https://forums.phpfreaks.com/topic/176868-what-to-use-instead-of-or/ Share on other sites More sharing options...
mikesta707 Posted October 7, 2009 Share Posted October 7, 2009 create an array of possible values, and use in_array $array = array("US", "AU", "UK", etc); if (in_array($country, $array)){ //do whatever } Quote Link to comment https://forums.phpfreaks.com/topic/176868-what-to-use-instead-of-or/#findComment-932575 Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 you can also look into switch() switch ($country) { case UK: //do something; break; case US: //do something; break; } you get the idea; Quote Link to comment https://forums.phpfreaks.com/topic/176868-what-to-use-instead-of-or/#findComment-932729 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 well since regardless of what country is, if it is either UK, US, etc, its going to do the same thing (based on OP's code) so a switch statement will probably be worse than what OP has now (worse meaning taking more code, and being rather sloppy) Quote Link to comment https://forums.phpfreaks.com/topic/176868-what-to-use-instead-of-or/#findComment-932736 Share on other sites More sharing options...
cags Posted October 8, 2009 Share Posted October 8, 2009 It all depends on the OP's needs. As mikesta707 says if your only checking against one set the in_array approach is probably best, if however you wish to do something different based on different sets you could use switch in this manner... <?php switch ($country) { case "UK": case "US": case "AUS": echo "English"; break; case "FR": case "CA": echo "French"; break; } ?> This will of course work the same as an if/elseif/else block, but some people find it easier to read at a glance. Obviously you could also create an array for each set and use in_array, the choice is up to the OP. Quote Link to comment https://forums.phpfreaks.com/topic/176868-what-to-use-instead-of-or/#findComment-932907 Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 well since regardless of what country is, if it is either UK, US, etc, its going to do the same thing (based on OP's code) so a switch statement will probably be worse than what OP has now (worse meaning taking more code, and being rather sloppy) i didn't catch the "I want to add 10-20 more ORs. Is there a better way to check these?" below the code .. i was just giving an example of alternatives to 'OR' .. obviously if every outcome is to be the same, then a switch() would not be practical .. in_array() is probably the best bet for sure. always good to give newbie's alternatives for everything, since hopefully now 'cursed' will read up on switch() and probably use it down the road ('cause switch() statements don't have to be messy .. i find, if done correctly, they can be an extremely clean system). Quote Link to comment https://forums.phpfreaks.com/topic/176868-what-to-use-instead-of-or/#findComment-933042 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.