President Obama Posted January 18, 2011 Share Posted January 18, 2011 I couldn't understand switch statements in .net and I still don't get them with php. Can someone turn this into a switch statement, I just can't wrap my head around then. if (isset($_POST['news'])){ add($_POST['text']); header("location: admin.php"); } elseif (isset($_POST['rnews'])){ deletenews($_POST['number']); header("location: admin.php"); } elseif (isset($_POST['taddm'])){ taddmember($_POST['website'], $_POST['name']); header("location: admin.php"); } elseif (isset($_POST['tdelm'])){ tdeletemember($_POST['number']); header("location: admin.php"); } elseif (isset($_POST['addm'])){ addmember($_POST['website'], $_POST['name']); header("location: admin.php"); } elseif (isset($_POST['delm'])){ deletemember($_POST['number']); header("location: admin.php"); } elseif (isset($_POST['tnews'])){ tadd($_POST['text']); header("location: admin.php"); } elseif (isset($_POST['trnews'])){ tdelete($_POST['number']); header("location: admin.php"); } elseif (isset($_POST['video'])){ videoupdate($_POST['link']); header("location: admin.php"); } else { echo " Failed"; } Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/ Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 This code does not lend itself to a switch very well because you are testing different values. Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161128 Share on other sites More sharing options...
President Obama Posted January 18, 2011 Author Share Posted January 18, 2011 God dam it, what good are switches for then? I thought thats what they were for. Bloody oath there confusing. Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161129 Share on other sites More sharing options...
btherl Posted January 18, 2011 Share Posted January 18, 2011 Switches are like a limited, inflexible version of if/then/else. They only work with one variable, and you have to list the exact values that variable can take. Let's say you use $_POST['mode'] to tell your script what mode to run in. Then you can do like this: switch($_POST['mode']) { case 'register': # Do register stuff break; case 'login': # Do login stuff break; default: # Handle unrecognized case } The advantage of a switch is that it enforces a good coding practice - it can reduce complexity of your code by forcing you to deal with a single variable with clearly defined values. You can't cheat with a switch because php won't allow it. Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161131 Share on other sites More sharing options...
President Obama Posted January 18, 2011 Author Share Posted January 18, 2011 Wait so, in a switch all the cases are ran? Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161135 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 Wait so, in a switch all the cases are ran? They could be, but that is what you use break for. Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161139 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 Your code could also be put in a switch, you would just need to test for the bool true. (I wouldn't generally recommend this though). switch (true) { case isset($_POST['news']): add($_POST['text']); header("location: admin.php"); break; case isset($_POST['rnews']): deletenews($_POST['number']); header("location: admin.php"); break; // etc etc } Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161140 Share on other sites More sharing options...
President Obama Posted January 18, 2011 Author Share Posted January 18, 2011 Oh ok I think I understand them a bit better now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161161 Share on other sites More sharing options...
btherl Posted January 18, 2011 Share Posted January 18, 2011 I definitely wouldn't recommend what's in thorpe's last post there That's not how switch was intended to be used. But apparently it does work. If you do use it, please document it to save the sanity of other people who see your code. Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161186 Share on other sites More sharing options...
President Obama Posted January 18, 2011 Author Share Posted January 18, 2011 Nah I didn't use it, that it was interesting to see it. Quote Link to comment https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/#findComment-1161226 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.