1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What is the difference between if.. else-if, else and switch statement

Discussion in 'PHP' started by webdeveloper24, Oct 4, 2012.

  1. #1
    I know the difference between if..elseif and switch statement but I have one confusion When I actually use switch case statement though I can solve it by using if..elseif and else statement.
     
    webdeveloper24, Oct 4, 2012 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Switch tests the same variable over and over

    if elseif allows you to test different variables

    eg
    switch($status) {
       case 'Open': 
          sendOpenEmail();
          break;
       case 'Closed':
          sendClosedEmail();
          break;
    }
    
    if ($status == 'Open') sendOpenEmail();
    elseif ($status == 'Closed' && $validEmail && $outcome == 'Accepted') sendClosedEmail();
    
    PHP:
     
    sarahk, Oct 6, 2012 IP
  3. Bhuvan14

    Bhuvan14 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if or else if is the better performance than switch statement. so use if else statement if u have need
     
    Bhuvan14, Oct 8, 2012 IP
  4. funty

    funty Greenhorn

    Messages:
    12
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    18
    #4
    both of them has similar work. you can do the same thing using if..else which u can do with switch but not viceversa for complex problems. Switch is not preferred usually by professional programmers, it is actually more compact, quite readable but still lot limited in functionality
     
    funty, Oct 9, 2012 IP
  5. Drent123

    Drent123 Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    All are conditional statement and switch used at the place for if-else for less memory utilization.
     
    Drent123, Oct 9, 2012 IP
  6. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #6
    they both work similar but have still small but important differences.

    switch does NOT support multiple conditions
    if ($var==true and $var2>10) {
    PHP:
    The above would not be possible to be easily and cleanly implemented with swutch.

    also you can use switch to share output between cases

    
    swicth ($var1) {
    
     case '1':
     case '3': echo $var1; break;
     case '2' : echo 'YAY ';
     case '4':
    case '5': echo 'YEEHAA'; brerak;
    }
    
    
    PHP:
    the above would result in this

    case 1: 1
    case 2: YAY YEEHAA
    case 3: 3
    case 4: YEEHAA
    case 5: YEEHAA


    so small differences but these differences decide when it is better to use if elseif or switch

    hope it helped.
     
    plussy, Oct 10, 2012 IP
  7. thegioivt

    thegioivt Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Both of them has similar work. If your conditions so much you should use swicth.
     
    thegioivt, Oct 10, 2012 IP