switch vs if

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

ketanco
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Apr 09, 2008 9:50 am

switch vs if

Postby ketanco » 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?

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

Re: switch vs if

Postby seandisanti » 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


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 4 guests

cron