shanali4 Posted May 18, 2010 Share Posted May 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/ Share on other sites More sharing options...
Daniel0 Posted May 18, 2010 Share Posted May 18, 2010 So actually what is difference between switch() and if statement. An if statement checks for a boolean, a switch statement checks one value against a number of other values. Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1059995 Share on other sites More sharing options...
ignace Posted May 18, 2010 Share Posted May 18, 2010 It's for example harder to accomplish the below with a switch statement: $value != 50 $value < 50 Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1059997 Share on other sites More sharing options...
Alex Posted May 18, 2010 Share Posted May 18, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060002 Share on other sites More sharing options...
Daniel0 Posted May 18, 2010 Share Posted May 18, 2010 $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'; } Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060005 Share on other sites More sharing options...
ignace Posted May 18, 2010 Share Posted May 18, 2010 I said it's harder not impossible. We know this, the OP doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060007 Share on other sites More sharing options...
Daniel0 Posted May 18, 2010 Share Posted May 18, 2010 I didn't say you said it was impossible Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060008 Share on other sites More sharing options...
ignace Posted May 18, 2010 Share Posted May 18, 2010 It was actually in response to Alex who said: "It's possible though" Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060009 Share on other sites More sharing options...
Alex Posted May 18, 2010 Share Posted May 18, 2010 I never thought you meant it was impossible either. I was just stating that for the purposes of the OP who might not have known that. Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060010 Share on other sites More sharing options...
ignace Posted May 18, 2010 Share Posted May 18, 2010 $php = 'switch ($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); LOL, what if I were to check multiple numbers? How fast does that execute actually? Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060013 Share on other sites More sharing options...
Daniel0 Posted May 18, 2010 Share Posted May 18, 2010 Dunno. I didn't execute it. I'm running on a 34-bit system, so ~PHP_INT_MAX == -9223372036854775808. It would take a lot of memory just generating the code. Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060024 Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 I'm running on a 34-bit system, That's leet! Does it use heptadecimal insread of hexadecimal? Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060031 Share on other sites More sharing options...
BizLab Posted May 18, 2010 Share Posted May 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060042 Share on other sites More sharing options...
kenrbnsn Posted May 18, 2010 Share Posted May 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060063 Share on other sites More sharing options...
ignace Posted May 18, 2010 Share Posted May 18, 2010 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060068 Share on other sites More sharing options...
Daniel0 Posted May 18, 2010 Share Posted May 18, 2010 I'm running on a 34-bit system, That's leet! Does it use heptadecimal insread of hexadecimal? Heh yeah, that was supposed to have been 64 Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1060079 Share on other sites More sharing options...
BizLab Posted June 1, 2010 Share Posted June 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/202141-what-is-difference-between-switch-and-if-statement/#findComment-1066113 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.