switch vs if

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 10
Joined: Wed Apr 09, 2008 9:50 am

Fri May 02, 2014 7:16 am

why do we use if statement at all?
isnt switch covering everything? isnt it more straightforward to use switch?

php-forum Fan User
php-forum Fan User
Posts: 974
Joined: Mon Oct 01, 2012 12:32 pm

Fri May 02, 2014 8:50 am

To test a single boolean expression, an if statement is less code, and easier to read. Switch in my opinion is the best option when you have more than 3 distinct options you need to handle, but I also believe that if you're holding yourself to good programming practices your code shouldn't be trying to do so many things at once to where a switch statement should ever really be necessary. If you want to make huge jumps in progress in the way you program, you should read this book http://www.amazon.com/s?search-alias=st ... 0132350882 The author, 'Uncle' Bob Martin lays out some very easy to follow rules, and explanations for them, and examples that will help you code smarter and faster in ANY language

Post Reply