Results 1 to 6 of 6

Thread: How to use Switch Case Expression in PHP?

  1. #1
    Join Date
    Aug 2006
    Posts
    209

    How to use Switch Case Expression in PHP?

    Hi friends,
    I am recently started doing coding with the PHP programming language. So you can consider me as a newbie. I have done some basic loops in PHP, and now I want to use the Switch Case. So please tell me how to use Switch Case Expression in PHP? If possible, please provide me the sample of coding so that it would be better for me to understand the use of Switch Case.
    Blessings to you

  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: How to use Switch Case Expression in PHP?

    I think that you should know about the Switch first. Later on you will feel easy to understand the Switch Case Expressions. The switch statement is equivalent to a series of if statements. On many occasions you will need to compare the same variable (or expression) with many different values, and execute different parts of code depending on which value it equals. This is exactly why is the switch statement. You should keep in mind that unlike other languages, the continue applies to switch and acts similar to break. If you have a switch inside a loop, and want to continue to the next iteration the outer loop, you must use continue 2.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Mar 2008
    Posts
    672

    Re: How to use Switch Case Expression in PHP?

    The following is an example of the switch structure, hope that it helps you to understand the necessary things :
    PHP Code:
    <?php
    if ($i == 0) {
        echo 
    "i equals 0";
    } elseif (
    $i == 1) {
        echo 
    "i equals 1";
    } elseif (
    $i == 2) {
        echo 
    "i equals 2";
    }
     
    switch (
    $i) {
        case 
    0:
            echo 
    "i equals 0";
            break;
        case 
    1:
            echo 
    "i equals 1";
            break;
        case 
    2:
            echo 
    "i equals 2";
            break;
    }
    ?>

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to use Switch Case Expression in PHP?

    It is important to understand that the switch statement executes each of the clauses in order. The switch statement executes line by line. At first, no code is executed. Only when a box is checked, then PHP executes instructions. PHP continues to execute instructions until the end of the block of the switch, or when it sees a break statement. If you do can not use the break statement at the end of the case statement, PHP will continue to execute all instructions that follow. In a switch, a condition is evaluated only once, and the result is compared to each box. In an elseif statement, the conditions are evaluated in each comparison.

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to use Switch Case Expression in PHP?

    According to me the Switch Case is easy to understand. In a switch, a condition is evaluated only once, and the result is compared to each box. In an elseif statement, the conditions are evaluated in each comparison. If your condition is more complicated than a simple comparison, or is part of a loop switch is faster. A special case is the default. This case is used when all other cases have failed. For example :
    PHP Code:
    <?php
    switch ($i) {
        case 
    0:
            echo 
    "i equals 0";
            break;
        case 
    1:
            echo 
    "i equals 1";
            break;
        case 
    2:
            echo 
    "i equals 2";
            break;
        default:
           echo 
    "i is not equal to 0, 1 or 2";
    }
    ?>

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: How to use Switch Case Expression in PHP?

    You can also use a semicolon instead of a colon after a case. The following example explains the same :
    PHP Code:
    <?php
    switch($fruits)
    {
        case 
    'apple';
        case 
    'mango';
        case 
    'pineapple';
            echo 
    'Good choice';
        break;
        default;
            echo 
    'Please make a new selection...';
        break;
    }
    ?>

Similar Threads

  1. Replies: 4
    Last Post: 14-04-2011, 02:06 PM
  2. How to break outer loop from switch case?
    By KALANI84 in forum Software Development
    Replies: 6
    Last Post: 09-10-2010, 06:59 PM
  3. Problem in switch case
    By Jensen Ackles in forum Software Development
    Replies: 5
    Last Post: 12-03-2010, 12:12 PM
  4. Apple Iphone 3G Case-Mate Case with Holster
    By Axeor in forum Portable Devices
    Replies: 1
    Last Post: 13-07-2009, 03:12 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,094,288.79203 seconds with 16 queries