Jump to content

Recommended Posts

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)

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.

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).

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.