User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 374,021 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,765 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1400 | Replies: 5
Reply
Join Date: May 2006
Location: United Kingdom
Posts: 19
Reputation: 100% is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
100%'s Avatar
100% 100% is offline Offline
Newbie Poster

If Or Switch

  #1  
May 15th, 2006
If .. Or Switch.

Which one is better? Are they used for different things?
Any pros or cons for each?

Or is it just a matter of which one you like best?

HTML/XHTML - OK
CSS - OK
JAVASCRIPT - Learning
PHP - Learning
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: somewhere in time
Posts: 71
Reputation: TopDogger is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
TopDogger's Avatar
TopDogger TopDogger is offline Offline
Junior Poster in Training

Re: If Or Switch

  #2  
May 16th, 2006
IMHO, "Switch" is better if you have to qualify a variable with a lot of different possible values and each requires a different process.

"If" is better if you only have to test for a few possible values. A long list of If-ElseIf statements gets to be pretty hard to read sometimes.

I generally use "Switch" if I need to test for three or more possible values.
Reply With Quote  
Join Date: Sep 2005
Posts: 611
Reputation: digital-ether will become famous soon enough digital-ether will become famous soon enough 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: If Or Switch

  #3  
May 20th, 2006
Like TopDogger said, switch is a bit more readable if you're going to branch code more than 3 ways depending on a value.

I would recommend you try using a "factory method" though if you have many different processes.
This is a bit of code that acts as a switch statement but does not get bulkier when you add more "switches".

[php]
/**
* Factory method example
*/
function factoryChoice($choice, $choices, $params = false) {
if ( in_array($choice, $choices) ) {
call_user_func($choice, $params);
} else {
echo 'Attempt to run arbitrary code!';
}
}

/**
* Example usage
*/
$choice = $_GET['choice'];
$choices = array('add', 'edit', 'delete');
$params = array($_GET['param1'], $_GET['param2'],$_GET['param3']);

factoryChoice($choice, $choices, $params);
[/php]

What the factory method essensially does is take in a choice String, then matches it with predetermined choices Array. If the choice is allowed, it calls a functions of the same name as the choice.

What this does effectively is prevent you from hardcoding your switch statements, but instead allow a dynamic choices Array.
The dynamic choices Array can be fed from hardcoded values, like in the example, or read from a db etc.

You can even go further and set different access levels on your choices quite easily.
This would be much harder with hard coded switch or else/if statement.

Hope this still pertains to the topic.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Oct 2004
Location: Romania
Posts: 48
Reputation: m-soft is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
m-soft m-soft is offline Offline
Light Poster

Re: If Or Switch

  #4  
May 20th, 2006
I use if more than switch.
It is a little common
Reply With Quote  
Join Date: Jul 2006
Posts: 14
Reputation: psalzmann is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
psalzmann psalzmann is offline Offline
Newbie Poster

Re: If Or Switch

  #5  
Jul 12th, 2006
I used a lot of if's, but lately I use them only for sub-section calling and use the switch() on handling the logic.

The good news is .. both will work just fine. Developer preference.
Reply With Quote  
Join Date: Jul 2004
Location: North East Indiana
Posts: 491
Reputation: Puckdropper is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 20
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: If Or Switch

  #6  
Jul 13th, 2006
The big advantage to switch statements is that they make your code cleaner in appearance, and thusly easier to maintain. If you have more than two nested IF statements, it's probably time to use a switch.
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 11:17 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC