[TUT]A-Z of Switch Case in PHP -> NO EXTERNAL LINK

Discussion in 'PHP' started by uworks, Feb 18, 2010.

  1. #1
    Switch case in PHP is an alternative to if else command. Switch case makes your code look more beautiful. The function of the code is actually to test a variable against a series of values until it finds a match, and it then executes the code corresponding to that match.
    Its syntax is :
    switch (condition)
    {
    case labelA:
      code to be executed if condition  = commandA;
      break;  
    case labelB:
      code to be executed if condition  = commandB;
      break;
    default:
      code to be executed
      if condition is different   from both commandA  and commandB;
    }
    Code (markup):
    It takes the condition mentioned in brackets and if the value of the condition = commandA then it will execute the code listed below case commandA and so on. If the value of condition is neither commandA or commandB then we use the default parameter. Let’s take one example :
    $dp= "digitalpoint";
    switch($dp)
    {
    case "facebook":
    echo "The value of \$dp is not equal to facebook ";
    break;
    
    case "orkut":
    echo "The value of \$dp is not equal to orkut ";
    break;
    
    case " digitalpoint ":
    echo "You are right value of \$dp is digitalpoint ";
    break;
    
    default:
    echo "You did’nt guess it right";
    break;
    }
    PHP:
    In this the value of $dp is set to digitalpoint and when the case is digitalpoint we get the result listed. Try changing the value of $dp (leaving facebook,orkut and digitalpoint) and when you execute it you get You did’nt guess it right result.
    Making it more simpler try :
    $dp="digitalpoint";
    switch ($dp)
    {
    case "orkut":
    case "facebook":
    case "google":
        echo "The value of \$dp is not orkut nor facebook neither google";
        break;
    
    case "digitalpoint";:
        echo "The value of \$dp is digitalpoint and you are right.";
    break;
    
    default:
    echo "You are wrong ";
    }
    PHP:
    This code takes $dp as the condition and checks if the value of $dp is equal to orkut,facebook or google it will display the same message. If the value of $dp is digitalpoint then it displays the message listed below the case. And then comes the default command which checks that if the value of $dp is not mentioned then the code after default will be displayed.

    Feedback appreciated
    Thanks,
    uworks
     
    uworks, Feb 18, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    sarahk, Feb 18, 2010 IP
  3. uworks

    uworks Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is just a simpler version.
     
    uworks, Mar 31, 2010 IP
  4. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Reader may mistakenly assume that break is required part of the switch/case syntax.
    That's why it's strongly recommended read official documentations instead of 'simpler versions'
     
    stOK, Apr 1, 2010 IP
    sarahk likes this.
  5. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks for teaching proper code cleanup/indenting/syntax... </sarcasm>
     
    krsix, Apr 1, 2010 IP