WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : SWITCH
Categories : Language constructs, PHP
Boaz Yahav
Date : May 15th 1999
Grade : 1 of 5 (graded 3 times)
Viewed : 5260
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Boaz Yahav
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

The SWITCH statement is similar to a series of IF statements on the
same expression. In many occasions, you may want to compare the
same variable (or expression) with many different values, and
execute a different piece of code depending on which value it equals
to. This is exactly what the SWITCH statement is for.

The following two examples are two different ways to write the same
thing, one using a series of IF statements, and the other using the
SWITCH statement:

<?
/* example 1 */

if ($i == 0) {
print "i equals 0";
}
if ($i == 1) {
print "i equals 1";
}
if ($i == 2) {
print "i equals 2";
}

/* example 2 */

switch ($i) {
case 0:
print "i equals 0";
break;
case 1:
print "i equals 1";
break;
case 2:
print "i equals 2";
break;
}
?>


It is important to understand how the SWITCH statement is executed
in order to avoid messups. The SWITCH statement executes line by
line (actually, statement by statement). In the beginning, no code is
executed. Only when a CASE statement is found with a value that
matches the value of the SWITCH expression, PHP begins to execute
the statements. PHP continues to execute the statements until the
end of the SWITCH block, or the first time it sees a BREAK statement.
If you don't write a BREAK statement at the end of a case's statement
list, PHP will go on executing the statements of the following case. For
example:

<?
/* example 3 */

switch ($i) {
case 0:
print "i equals 0";
case 1:
print "i equals 1";
case 2:
print "i equals 2";
}
?>


Here, if $i equals to 0, PHP would execute all of the print statements!
If $i equals to 1, PHP would execute the last two print statements, and
only if $i equals to 2, you'd get the 'expected' behavior and only 'i
equals 2' would be displayed. So, it's important not to forget BREAK
statements (even though you may want to avoid supplying them on
purpose under certain circumstances).

A special case is the default case. This case matches anything that
wasn't matched by the other cases. For example:

<?
/* example 4 */

switch ($i) {
case 0:
print "i equals 0";
break;
case 1:
print "i equals 1";
break;
case 2:
print "i equals 2";
break;
default:
print "i is not equal to 0, 1 or 2";
}
?>


Another fact worth mentioning is that the CASE expression may be any
expression that evaluates to a scalar type, that is, integer or real
numbers and strings. Arrays or objects won't crash PHP, but they're
meaningless in that context.



Constants
Categories : Language constructs, PHP
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Calendar using Date function
Categories : HTML and PHP, PHP, Date Time, Calendar
Invision Forums Latest Threads list
Categories : PHP, Miscellaneous, Databases, MySQL
Using PHP im HTML image tags
Categories : PHP, HTML and PHP, Graphics, Beginner Guides
IP Blocking
Categories : PHP, Security, HTTP
Pseudo Non Parsed Header. Output to the the browser as the script runs.
Categories : PHP, HTTP, HTML and PHP
UDMSearch - a free search engine, indexing system.
Categories : Search Engines, Linux, PHP, MySQL, ODBC
how to check if a string contains a letter from a different language?
Categories : PHP, Regexps, Languages
Unix Disk Information with graphs
Categories : PHP, Shell Scripting, Filesystem
fforum fumanchi forum MySQL treestructure
Categories : Complete Programs, PHP, MySQL
ibase_close -- Close a connection to an InterBase database
Categories : PHP, PHP Functions, InterBase
Display variables when a form is submitted using POST/GET
Categories : PHP, Functions, Variables, Debugging
AJAX Data Grid System using php and mysql. A complete login system with the ability to display data in a grid using ajax. Add , update and delete the records without reloading the page.
Categories : PHP, AJAX, Databases, MySQL, Java Script
EAvalidator - This class can be used to validate an e-mail address by checking its domain.
Categories : PHP, PHP Classes, Email, Regexps
 Marco Catani wrote : 415
SWITCH should be similar to IF it is similar only if you 
compare the value of a variable with an interger, if you 
try the following:

switch ($i) { 
    case `a`: 
        print "i equals a"; 
        break; 
    case `b`: 
        print "i equals b"; 
        break; 
    case `c`: 
        print "i equals c"; 
        break; 
}

it doesn`t work at all.
Cheers,
 Marco
 
 Henrik Hansen wrote : 1128
To Marco...

Marco wrote:
"switch ($i) { 
case `a`: 
print "i equals a"; 
break; 
case `b`: 
print "i equals b"; 
break; 
case `c`: 
print "i equals c"; 
break; 

"

Try to put a "$" sign in front of the "i" in the 
print statment...! Like:
"  case `a`: 
     print "$i equals a"; 
     break; "

That works for me... ;O)
Henrik
 
 Henrik Hansen wrote :1129
Or better...
"
 case `a`: 
   print $i . ` equals a`; 
   break; 
"
This make the php parser a bit faster... ;o)
Henrik