boby Posted August 26, 2007 Share Posted August 26, 2007 Hello guys! I am working on a script and asking myself which way to go. The script is for adding a new entry to the DB or editing an existing entry. Some of the fields are the same, but I cannot populate the fields that I use to add/edit an entry at the top and then populate if user is editing or if adding new. So, I am now asking what would be better, many IF clauses or a SWITCH? ...do common stuff... if ($action == 'edit') ...do edit stuff... ...do common stuff... if ($action == 'edit') ...do edit stuff... ...do common stuff... if ($action == 'new') ...do new entry stuff... or use the SWITCH like: switch (action) { case 'edit' : ...do edit stuff... ...do common stuff... ...do common stuff... ...do edit stuff... break; case 'new' : ...do common stuff... ...do common stuff... break; } If I choose the IF clause, the whole code is here and there. With the switch it would be more structured but I would have some things to write twice once for edit and the same in the new entry case. Thanks for any help! Regards, Boby Later Edit: Or should I use two different files? Here I would have again the same thing, both would have part of the code identical. Quote Link to comment https://forums.phpfreaks.com/topic/66704-solved-if-vs-switch/ Share on other sites More sharing options...
pocobueno1388 Posted August 26, 2007 Share Posted August 26, 2007 If you are going to have more than 3 IF statements, I would say use the switch. If there aren't THAT many IF statements, it really shouldn't matter which one you use. Quote Link to comment https://forums.phpfreaks.com/topic/66704-solved-if-vs-switch/#findComment-334256 Share on other sites More sharing options...
448191 Posted August 26, 2007 Share Posted August 26, 2007 If you have many conditionals you likely have a candidate for subclassing. But if you must choose between many if statements or switch, switch is usually the more readable an efficient option. Quote Link to comment https://forums.phpfreaks.com/topic/66704-solved-if-vs-switch/#findComment-334415 Share on other sites More sharing options...
boby Posted August 26, 2007 Author Share Posted August 26, 2007 Thank you very much guys! I'll go with the switch Quote Link to comment https://forums.phpfreaks.com/topic/66704-solved-if-vs-switch/#findComment-334455 Share on other sites More sharing options...
kathas Posted August 26, 2007 Share Posted August 26, 2007 Maybe a little late but switch is not only the option to go because it makes your code more readable it also makes your code faster! Using switch the value of the variable ($action in our case) is only evaluated once while using multiple ifs it is evaluated every time a condition is encountered... Regards, Kathas Quote Link to comment https://forums.phpfreaks.com/topic/66704-solved-if-vs-switch/#findComment-334459 Share on other sites More sharing options...
448191 Posted August 26, 2007 Share Posted August 26, 2007 But if you must choose between many if statements or switch, switch is usually the more readable and efficient option. Quote Link to comment https://forums.phpfreaks.com/topic/66704-solved-if-vs-switch/#findComment-334463 Share on other sites More sharing options...
kathas Posted August 28, 2007 Share Posted August 28, 2007 But if you must choose between many if statements or switch, switch is usually the more readable and efficient option. True! next time I'll read more carefully... Quote Link to comment https://forums.phpfreaks.com/topic/66704-solved-if-vs-switch/#findComment-336222 Share on other sites More sharing options...
roopurt18 Posted August 28, 2007 Share Posted August 28, 2007 I was going to add that you can prevent repeating code by wrapping it in a function. Then at most you're repeating one line, i.e. the function call. Quote Link to comment https://forums.phpfreaks.com/topic/66704-solved-if-vs-switch/#findComment-336294 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.