Bricktop Posted January 23, 2009 Share Posted January 23, 2009 Hi chaps, I have some code as follows: if ($_GET['function'] == "") { $content .= 'Foo'; echo $content; } else if ($_GET['function'] == "1") { $content .= 'Foo'; echo $content; } else if ($_GET['function'] == "2") { $content .= 'Foo'; echo $content; } else if ($_GET['function'] == "3") { $content .= 'Foo'; echo $content; } else if ($_GET['function'] == "4") { $content .= 'Bar'; echo $content; } else if ($_GET['function'] == "5") { $content .= 'Bar'; echo $content; } else if ($_GET['function'] == "6") { $content .= 'Bar'; echo $content; } else { $content .= 'woot'; echo $content; } The above works great, but as you can see I'm reusing the same code and making the file very large when it doesn't need to be. So, I tried the following: if ($_GET['function'] == "") { $content .= 'Foo'; echo $content; } else if ($_GET['function'] == "1" || "2" || "3") { $content .= 'Foo'; echo $content; } else if ($_GET['function'] == "4" || "5" || "6") { $content .= 'Bar'; echo $content; } else { $content .= 'woot'; echo $content; } The above code always defaults to "Foo". Am I doing it right? Or am I missing something really obvious? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/ Share on other sites More sharing options...
dvd420 Posted January 23, 2009 Share Posted January 23, 2009 Hi you should use else if ($_GET['function'] == "1" || $_GET['function'] == "2" || $_GET['function'] == "3") Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/#findComment-744142 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 i would also suggest a switch statement: <?php /* This says "if 'function' is 'set' or exists, then set $func to that value, otherwise set $func to null" (this is only really to rid yourself of any "Notice: Undeclared Variables" warnings.) */ $func = (isset($_GET['function']))? $_GET['function'] : null; // This is like a clean and easy if statement block, with the "else" being "default". Switch($func){ default: // Put any code here that would be displayed if function did not match any criteria break; case "1": // Maybe Include("file.php"); break; case "2": // Include("file2.php"); break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/#findComment-744148 Share on other sites More sharing options...
hobeau Posted January 23, 2009 Share Posted January 23, 2009 i would also suggest a switch statement: I agree. Switch statements are faster than if/else statements. Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/#findComment-744151 Share on other sites More sharing options...
Bricktop Posted January 23, 2009 Author Share Posted January 23, 2009 uniflare, thanks for the help, and thanks everyone else for your kind replies. I've converted my script using uniflare's code but I'm still getting the same problem. I have changed it to: <?php $func = (isset($_GET['function']))? $_GET['function'] : null; Switch($func){ default: $content .= 'woot'; echo $content; break; case "1" or "2" or "3": $content .= 'Foo'; echo $content; break; case "4" or "5" or "6": $content .= 'Bar'; echo $content; break; } ?> Using the above code it always defaults to "Foo", even when the function the script is using is 4, 5 or 6. It will display "woot" however when the function is null. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/#findComment-744162 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 There was a problem with your use or or, the following works; <?php $func = (isset($_GET['function']))? $_GET['function'] : null; $func = (int) $func; Switch($func){ case 1: case 2: case 3: $content = 'Foo'; echo $content; break; case 4: case 5: case 6: $content = 'Bar'; echo $content; break; default: $content = 'Foo'; echo $content; break; } Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/#findComment-744164 Share on other sites More sharing options...
RussellReal Posted January 23, 2009 Share Posted January 23, 2009 idk if u could use 'or' laik dat lol Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/#findComment-744165 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 Supposidly you can use the following; case (1 || 2 || 3): But it didn't work for me, and I don't much like it, lol Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/#findComment-744170 Share on other sites More sharing options...
Bricktop Posted January 23, 2009 Author Share Posted January 23, 2009 Thank you gevans - your code works a treat! And thank you to everyone else. Topic Solved Quote Link to comment https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/#findComment-744176 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.