Navigation :
HomeSource code
Tutorials
RSS feeds
Articles
Wordpress plugins
Books
Sample Chapters
Software
Downloads
Hosting
manuals
Script directory
Training
Our Links
Site Sponsors :
Bookmark :
Links :
software directorydropshippers
domain names
Downloads :
dzphp editorEasyPHP
mysql_phpgenerator
php-studio-trial
phpdesigner_7_2_5
phpMyAdmin-3.4.3.2
/phptriad2-2-1
phpxedit_321
rapidphp 2011
WampServer2.1d-x64
WampServer2.1e-x32
xampp-win32-1.7.4-VC6-installer
xampplite-win32-1.7.3
Misc :
Webmaster ResourcesOnly PHP
ScriptSearch.com
Scripts.com - Get the best scripts NOW!
AndreaPHP Programming
Switch Cases
How They Work
Here's an example of a simple game where a user enters a $user_command and different functions are run as a result:
if($user_command == "n")
{
go_north();
}
else if($user_command == "e")
{
go_east();
}
else if($user_command == "s")
{
go_south();
}
else if($user_command == "w")
{
go_west();
}
else
{
do_something_else();
}
Clearly, there's a lot of repeated code here. The switch case structure allows you to avoid this redundant code. It allows programmers to repeatedly compare the value of a certain variable to a list of possible values and execute code based on the result. This is the syntax for a switch case statement, compared to the same code written using if statements:
| if statement style | switch case style |
|---|---|
| if($firstvariable == 'comparison1' || $firstvariable == 'comparison2') { doSomething(); doSomethingElse(); } else if ($firstvariable == 'comparison3') { doAThirdThing(); } else { launchMissiles(); } | Look at how much switch case saves you! switch($firstvariable) { case 'comparison1': case 'comparison2': doSomething(); doSomethingElse(); break; case 'comparison3': doAThirdThing(); break; default: launchMissiles(); break; } |
The switch case style will save you from retyping $firstvariable, and make your code look cleaner (especially if that code is a long chain of simple if statements). Returning to our zork sample program, we have:
| Original Code | Switch-Case Code |
|---|---|
| if($user_command == "n") { go_north(); } else if($user_command == "e") { go_east(); } else if($user_command == "s") { go_south(); } else if($user_command == "w") { go_west(); } else { do_something_else(); } | switch($user_command) { case 'n': go_north(); break; case 'e': go_east(); break; case 's': go_south(); break; case 'w': go_west(); break; default: do_something_else(); break; } |
Syntax switch($var) { case [value]: [code] break; case [value]: [code] break; ... default: [code] break; }
In this example. $var is the first variable to be compared. This variable is then compared against each case statement from the top down, until it finds a match. At that point, the code will excecute until a break statement is reached (which will allow you to leave the case statement entirely).
Important Warning about Using Switch Case Statements
Don't forget to use break when you mean break! If you forget, you might run functions you don't intend to. However, there are circumstances where leaving breaks out can be useful.
Consider this example:
switch ($n)
{
case 0: case 1: case 2: //only executes if $n is 0, 1 or 2 doSomethingForNumbers2OrSmaller();
break;
case 3: //only executes if $n is 3
doSomethingForNumber3();
default:
//only executes if $n is 3 or above
doSomethingForNumbers3OrBigger();
break;
}
This kind of coding is sometimes frowned upon, since it's not always as clear to see what the code is meant to do. Also, consider commenting case statements that aren't supposed to have a break; statement before the next case, so when others look at your code, they know not to add a break
All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks
Books :
Sponsors :
Random Code :
Check number of characters(string)Check server status(internet)
Show MySQL tables(database)
Random Article :
PHP Dynamic ContentMaguma has integrated support for the eBay Acellerator Toolkit for PHP PHP AT in their PHP IDE Maguma Workbench
Network:
Programming resourcesTutorials directory
Hosting resources
ASP site
Domain names
Progged
Maxi directory
bigarticle : free articles
A Code
Code N Tutorials
Get PHP
Programmers help
Del.icio.us
Digg
Furl
Newsvine
Netscape
Reddit
StumbleUpon
Technorati
Squidoo
Windows Live
Yahoo MyWeb
Ask
Google

