Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> PHP >> PHP Switch Statement
PHP Switch Statement
PHP Switch Statement


by Arun Singh on 9/7/2011 6:03:51 PM

Views: 1179       Comments: 0

PHP Switch Statement

‘Switch’ statement is conditional statement used to perform different action based on different condition. I.e. switch statement is used to select one of many blocks of code to be executed.

The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body.

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.

Syntax:

switch (expression)

        {

           case label_1:

           code to be executed if expression = label_1 ;

           break;

           case label_2:

           code to be executed if expression = label_2;

           break;

           .............

           .............

 

           default:

           code to be executed if expression is not equal to all label_1, label_2,... etc.

         

        }

 

Let’s we have an example, how to implement switch statement in PHP.

Example:

<html>

<head>

       <title> Switch Statement</title>

       </head>

<body>

       <?php

              // Declare a variable which is stores product name

              $product_Name = "Mouse";

 

              switch (    $product_Name )

                {

                     case "Monitor" :

                           echo "This is monitor section";

                           break;

                     case "Keyboard" :

                           echo "This is Keyboard section";

                           break;

                     case "CPU" :

                           echo "This is CPU section";

                           break;

                     case "Mouse" :

                           echo "This is mouse section";

                           break;

                     default:

                     echo "All above block of code not executed this is default section";

                }

       ?>

</body>

 

</html>

Desired Output:

PHP Switch Statement

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2888
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.