Results 1 to 3 of 3

Thread: Switch Statement in PHP

  1. #1
    Join Date
    May 2008
    Posts
    21

    Switch Statement in PHP

    PHP switch statement is used when one of the several options or a groups of options are to be executed based on some conditions. We can use several PHP if conditions with php if else but this is a better way of coding when we know one of the several options is correct. (Repeated checking by if condition can be avoided)

    If you want to select one of many blocks of code to be executed, use the Switch statement.

    The switch statement is used to avoid long blocks of if..elseif..else code.

    Syntax

    PHP Code:
    switch (expression)
    {
    case 
    label1:
      
    code to be executed if expression label1;
      break;  
    case 
    label2:
      
    code to be executed if expression label2;
      break;
    default:
      
    code to be executed
      
    if expression is different 
      from both label1 
    and label2;


  2. #2
    Join Date
    May 2008
    Posts
    21

    Re: Switch Statement in PHP

    A single expression (most often a variable) is evaluated once. The value of the expression is compared with the values for each case in the structure. If there is a match, the code associated with that case is executed. After a code is executed, break is used to stop the code from running into the next case. The default statement is used if none of the cases are true.

    PHP Code:
    <html>
    <body>

    <?php
    switch ($x)
    {
    case 
    1:
      echo 
    "Number 1";
      break;
    case 
    2:
      echo 
    "Number 2";
      break;
    case 
    3:
      echo 
    "Number 3";
      break;
    default:
      echo 
    "No number between 1 and 3";
    }
    ?>

    </body>
    </html>

  3. #3
    Join Date
    May 2008
    Posts
    115

    Re: Switch Statement in PHP

    PHP Code:
    $destination "Tokyo";
    echo 
    "Traveling to $destination<br />";
    switch (
    $destination){
        case 
    "Las Vegas":
            echo 
    "Bring an extra $500";
            break;
        case 
    "Amsterdam":
            echo 
    "Bring an open mind";
            break;    
        case 
    "Egypt":
            echo 
    "Bring 15 bottles of SPF 50 Sunscreen";
            break;    
        case 
    "Tokyo":
            echo 
    "Bring lots of money";
            break;
        case 
    "Caribbean Islands":
            echo 
    "Bring a swimsuit";
            break;    

    Display:

    Traveling to Tokyo
    Bring lots of money

Similar Threads

  1. How to use switch statement in java
    By Raju Chacha in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 06:40 PM
  2. Java program to use enum in switch statement.
    By MADGE25 in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 06:12 PM
  3. Need Codings for PHP Switch Statement?
    By NGV BalaKrishna in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 10:31 PM
  4. JavaScript Switch Statement
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 03:33 PM
  5. SWITCH....CASE statement in C#
    By Aadarsh in forum Software Development
    Replies: 1
    Last Post: 10-11-2008, 05:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,743,585.96514 seconds with 17 queries