Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

switch vs if

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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?
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Apr 09, 2008 9:50 am

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 ... 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
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.