VisualBuilder
  Home > Php > Tutorials > The Switch Statement - PHP Tutorial
Tell a friend
Link to us
Total Members
      Members: 84661
     
Sitemap Forum Chat
Home
PHP Tutorial Home
1 . An Introduction to PHP
2 . Getting Started With Variables
3 . Comments in PHP
4 . Operators
5 . The Switch Statement
6 . Logical Operators
7 . Boolean Values
8 . Working with HTML Forms
9 . Programming Loops
10 . Arrays in PHP
11 . String Manipulation
12 . Create your own Functions
13 . Date and Time Functions in PHP
14 . Working With Files In PHP
 
 
PHP Home
PHP Members (3076)
PHP Member Articles ( 9 )
PHP Discussion (11)
PHP Q & A ( 13 )
- PHP Ask Question
- PHP Questions
- PHP Unanswered Questions
PHP Resources
PHP Source Code (463)
PHP Articles (118)
PHP Blogs (30)
PHP Jobs (0)
PHP Components (24)
PHP Books (3)
PHP Websites (29)
PHP News (17)
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.Net Security,Internationalisation And Deployment
ASP.NET Tutorial
C# Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (451 )
Source Code (3275 )
Articles (359 )
Books (372 )
Components (1596 )
News (892 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Php Tutorial
 The Switch Statement
  << Prev: Operators Next: Logical Operators >>

The Switch Statement

We tested two variables in the previous examples. A flag of a country was shown on the screen,depending on the value inside of the variable. A long list of if and else … if statements were used. A better option,if you have only one variable to test,is to use something called a switch statement. To see how switch statements work,check the following example.


<?


$ukFlag ='1';


switch ( $ukFlag )


{
   case '0':
      echo "<IMG SRC=images/usaFlag.jpg>";


      break;


   case '1':
      echo "<IMG SRC=images/usaFlag.jpg>";


      break;


   default:
      echo "No Flag Shown";
}


?>


 


switch ($ukFlag)


You Start with the word 'Switch' then variable name you want to check.


 


case 'What you want to check for':


The word 'case' is used before each value you want to check for. These value are: 0 and 1. These are the values we need after the word 'case'. After the the text or variable you want to check for,a colon is needed ( : ) and code written after this.


 


break;
You need to tell PHP to "Break out" of the switch statement. If you don't,PHP will simply drop down to the next case and check that. Use the word 'break' to get out of the Switch statement.



default:
If nothing case matched than the value that you have under default statement will execute.




  << Prev: Operators Next: Logical Operators >>
Php Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
 
ADVERTISEMENT
Partner List
Code Project
ASP Alliance
More
 
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising