Jump to content

Checking for multiple values in a Switch statement


theITvideos

Recommended Posts

Hi there,

 

I am working on a PHP web form.

 

There is simple textbox where users can enter countries.

 

If the user enters any of the European countries for example Spain, Germany or Italy then the web page must echo ' You entered a European country. The code I am using is:

 

switch ($txtCountry) { 
            case 'Germany' || 'Spain' || 'Belgium' || 'Cyprus' || 'Estonia': 
            echo "You entered a European Country";
            break; 

            case 'Japan': 
            echo "You entered a Far Eastern Country";
            break;                 

            default: 
            echo "Unknown Country";
            break; 
        }

 

Now the problem is even if I enter a different country like Japan, it goes to the first Case: i.e. 'You entered a European Country'

 

Whats the best way to use Switch case to check multiple values. Is my Syntax correct or do I need to use single quote of double quote here.

 

Please reply.

 

Thank you! :)

Link to comment
Share on other sites

switch ($txtCountry) { 
case "Germany":
case "Spain":
case "Belgium":
case "Cyprus":
case "Estonia": 
	echo "You entered a European Country";
break; 

case "Japan": 
	echo "You entered a Far Eastern Country";
break;                 

default: 
	echo "Unknown Country";
break; 
}

Link to comment
Share on other sites

switch ($txtCountry) { 
case "Germany":
case "Spain":
case "Belgium":
case "Cyprus":
case "Estonia": 
	echo "You entered a European Country";
break; 

case "Japan": 
	echo "You entered a Far Eastern Country";
break;                 

default: 
	echo "Unknown Country";
break; 
}

 

Thank you for your reply. I had a feeling that I'd be something like this.

 

Now I have over 30 countries, and it would look very neat to have one case for one country.

 

Can we use if else statement? to make it little neat.

 

Can we try something like:

 

if ($txtCountries in ('Germany' || 'Spain' || 'Belgium' || 'Cyprus' || 'Estonia') )
{
echo "You selected European Country";
}
else
{
echo "other country...";
}

 

Whats the correct syntax for this type of if-else statement.

 

Please reply.

 

Thank you. :)

Link to comment
Share on other sites

Yeah, as said you can put them in an array and then check if the country is in the array. Here's how you can do it with multiple categories:

 

$european = array("Germany", "Spain", "Belgium", "Cyprus", "Estonia");
$far_eastern = array("Japan");

if (in_array($txtCountries, $european))
echo "You entered a European Country";
else if (in_array($txtCountries, $far_eastern))
echo "You entered a Far Eastern Country";
else
echo "Unknown Country";

 

If you want to use a switch statement you could do it like this:

 

$european = array("Germany", "Spain", "Belgium", "Cyprus", "Estonia");
$far_eastern = array("Japan");

if (in_array($txtCountries, $european))
$country = "european";
else if (in_array($txtCountries, $far_eastern))
$country = "far_eastern";

switch ($country) { 
case "european": 
	echo "You entered a European Country";
break; 

case "far_eastern": 
	echo "You entered a Far Eastern Country";
break;                 

default: 
	echo "Unknown Country";
break; 
}

Link to comment
Share on other sites

$euro_countries = array('Germany', 'Spain', 'Belgium', 'Cyprus', 'Estonia');

if (in_array($txtCountries, $euro_countries)) {
{
echo "You selected European Country";
}
else
{
echo "other country...";
}

 

Thanks for the reply. This is very strange. I type the code exactly has you mentioned. i.e. creating an array of countries. I typed:

$euro_countries = array('Albania' || 'Andorra' || 'Germany' || 'Austria' || 'Bulgaria' || 'Croatia' || 'Cyprus' || 'Czech Republic');
if (in_array($txtCountry, $euro_countries)) 
        {
           echo "You selected European Country";
        }
        elseif($txtCountry == 'USA')
        {
           echo "You selected USA";
        }
        else
        {
            echo "Unknown";
        }

 

The control always still goes to the first condition, even if I type a non-European country like when I type USA. It says 'You selected European Country' Then I checked to see what shows in the $euro_countries Array.

 

I did the print_r()

 

print_r($euro_countries);

 

I get:

 

Array ( [0] => 1 ) 

 

Are we missing something here.

 

Cheers! :)

Link to comment
Share on other sites

Ok I checked, if I only have one element in the array then it compares the European country correctly.

 

If there are more countries in the array i.e

 

$euro_countries = array('Germany', 'Spain', 'Belgium', 'Cyprus', 'Estonia');

then as crazy as it may sound, it just satisfies the condition and says 'it is a European country' even it enter Japan or some other non-european country.

 

On the other hand, it if I just have an array with one element as:

 

$euro_countries = array('Germany');

Now it actually checks if I entered a european country when I have only one element in the array.

 

Why it always meets the condition to true with multiple elements of european countries in the array even if I type non-european country.

 

Is it something to do with the array list here?

 

 

Link to comment
Share on other sites

Paste in your current code block . . .

 

I pasted the code earlier, here it is again:

$euro_countries = array('Albania' || 'Andorra' || 'Germany' || 'Austria' || 'Bulgaria' || 'Croatia' || 'Cyprus' || 'Czech Republic');
if (in_array($txtCountry, $euro_countries)) 
        {
           echo "You selected European Country";
        }
        elseif($txtCountry == 'USA')
        {
           echo "You selected USA";
        }
        else
        {
            echo "Unknown";
        }

 

But now I figured out a way which is not so efficient but it gives the result:

if ($txtCountry == 'Italy' || $txtCountry == 'Germany' || $txtCountry == 'Spain' || $txtCountry == 'Poland' || $txtCountry == 'Sweden' || $txtCountry == 'France') 
        {
           echo "You selected European Country";
        }
        elseif($txtCountry == 'USA')
        {
           echo "You selected USA";
        }
        else
        {
            echo "Unknown";
        }

 

yeah this works for me, but I guess not so effecient way of doing it. Array thing would've been better. But i guess... as long as it works, it works.

 

What do you think?

Link to comment
Share on other sites

$euro_countries = array('Albania', 'Andorra', 'Germany',  'Austria',  'Bulgaria', 'Croatia',  'Cyprus', 'Czech Republic');
if (in_array($txtCountry, $euro_countries)) 
        {
           echo "You selected European Country";
        }
        elseif($txtCountry == 'USA')
        {
           echo "You selected USA";
        }
        else
        {
            echo "Unknown";
        }

Link to comment
Share on other sites

Look at your array definition. You're using the || where commas should be . . .

 

gosh how ignorant of me, i spent a good time on this crazy thing. It was just a tiny  smaalll things.

 

Now it works like a charm! Good that you pointed this out! :)

 

Bravo!! :D

Link to comment
Share on other sites

$euro_countries = array('Albania', 'Andorra', 'Germany',  'Austria',  'Bulgaria', 'Croatia',  'Cyprus', 'Czech Republic');
if (in_array($txtCountry, $euro_countries)) 
        {
           echo "You selected European Country";
        }
        elseif($txtCountry == 'USA')
        {
           echo "You selected USA";
        }
        else
        {
            echo "Unknown";
        }

 

How sweeet of you. thank you very much! I so very appreciate. It is now working smoothly.

 

8)

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.