Jump to content

What is difference between Switch() and If statement ?


shanali4

Recommended Posts

In the following code I've used if statement

<?php
$country="";
if ($country=="Pakistan")
echo "Hi, You have selected Pakistan";
elseif ($country=="India")
echo "Hi, You have selected India";
elseif ($country=="United states")
echo "Hi you have selected US";
else
echo "Your country is not in our list";
?>

 

Now in the following code i've used PHP switch () function to accomplish the same task as by if statement

 

<?php
$country="";
switch ($country)
{
case "Pakistan";
echo "Hi, You have selected Pakistan";
break;
case "India":
echo "Hi, You have selected India";
break;
case "United states":
echo "Hi you have selected US";
break;
default:
echo "Your country is not in our list";
}
?>

 

If we add Pakistan, India or united state in $country variables in both of the codes and execute them the result would be same in case of both if statement and switch ().

 

So actually what is difference between switch() and if statement.

 

Waiting 4 reply.

 

Link to comment
Share on other sites

It's for example harder to accomplish the below with a switch statement:

 

$value != 50

$value < 50

It is possible though, with some atypical syntax.

 

switch(true)
{
    case $value != 50:
        // ...
        break;
}

Link to comment
Share on other sites

$value != 50

 

switch ($value) {
    case 50:
        break;
    default:
        echo 'it is not 50';
}

 

$value < 50

 

$php = 'switch (floor($value)) {';
for ($i = 49; $i > ~PHP_INT_MAX; --$i) {
    $php .= "\n\tcase {$i}:";
}
$php .= "\n\t\techo 'it is less than 50';\n}";
eval($php);

 

Or like this, but that's cheating seeing as you're not using $value on the switch:

 

switch (true) {
    case ($value < 50):
        echo 'it is less than 50';
}

Link to comment
Share on other sites

To the original question :

 

In the following code I've used if statement

<?php
$country="";
if ($country=="Pakistan")
echo "Hi, You have selected Pakistan";
elseif ($country=="India")
echo "Hi, You have selected India";
elseif ($country=="United states")
echo "Hi you have selected US";
else
echo "Your country is not in our list";
?>

 

Now in the following code i've used PHP switch () function to accomplish the same task as by if statement

 

<?php
$country="";
switch ($country)
{
case "Pakistan";
echo "Hi, You have selected Pakistan";
break;
case "India":
echo "Hi, You have selected India";
break;
case "United states":
echo "Hi you have selected US";
break;
default:
echo "Your country is not in our list";
}
?>

 

If we add Pakistan, India or united state in $country variables in both of the codes and execute them the result would be same in case of both if statement and switch ().

 

So actually what is difference between switch() and if statement.

 

Waiting 4 reply.

 

 

The biggest difference you will see on this scale is how easily the conditional can be read. I would absolutely use the switch statement for your country selection box (its slightly faster on large scale switches, but this is insignificant here).

 

So, the bottom line: use switch because it is easier to understand while reviewing

 

PS DON'T FORGET TO ADD THE LAST BREAK TO YOUR DEFAULT CONDITION

Link to comment
Share on other sites

You don't need to put a "break" statement in the last condition, since it is the last condition. The reason you have a "break" statement in the other conditions is to prevent PHP from executing any other statements after the one that matches. The "break" statement is not required in each condition. You can have something like:

<?php
switch ($value) {
    case 'a':
         $a = "do something for a";
    case 'b':
         $b = "do something for b";
         $both = "do something for both a & b";
         break;
    default:
         $n = "neither a or b";
}
?>

 

Ken

Link to comment
Share on other sites

<?php
switch ($value) {
    case 'a':
         $a = "do something for a";
    case 'b':
         $b = "do something for b";
         $both = "do something for both a & b";
         break;
    default:
         $n = "neither a or b";
}
?>

 

It may be not so clear as to what Ken was trying to explain but when you pass the value a, both a and b will execute. Not default as a break has been added before default: When you pass the value b only b is executed.

 

What I'm trying to say is that, if you don't write the break it will fall-through (and execute all code in between) until a break is reached or the switch has ended.

Link to comment
Share on other sites

  • 2 weeks later...

You don't need to put a "break" statement in the last condition, since it is the last condition. The reason you have a "break" statement in the other conditions is to prevent PHP from executing any other statements after the one that matches. The "break" statement is not required in each condition. You can have something like:

<?php
switch ($value) {
    case 'a':
         $a = "do something for a";
    case 'b':
         $b = "do something for b";
         $both = "do something for both a & b";
         break;
    default:
         $n = "neither a or b";
}
?>

 

Ken

 

I like to keep the code clean and consistent. To the original post, you will want to write switches with breaks after every condition you are comparing. Otherwitse the IF() conditional is more well suited.. it's good form to add the lastbreak, whether needed (technically) or not. THANKS

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.