Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    elseif or switch/case ?

    Okay, whats the difference of using either or? cons/pros ? I believe they almost can have the same idea, I'd rather use "switch/case" but wanted to see what everyone else thought about it


    i'd prefer.
    PHP Code:
    switch($order)
    {
    case 
    'ID':$gameOrder $this->type0;break;
    case 
    'DATE':$gameOrder $this->type1;break;
    case 
    'RANDOM':$gameOrder $this->type2;break;
    case 
    'RATING':$gameOrder $this->type3;break;
    case 
    'TOTALPOPULAR':$gameOrder $this->type4;break;
    case 
    'TODAYPOPULAR':$gameOrder $this->type5;break;

    or

    PHP Code:
    if($type=='ID'){$type $this->type0;}
    elseif(
    $type=='DATE'){$type $this->type1;}
    elseif(
    $type=='RANDOM'){$type $this->type2;}
    elseif(
    $type=='RATING'){$type $this->type3;}
    elseif(
    $type=='TOTALPOPULAR'){$type $this->type4;}
    elseif(
    $type=='TODAYPOPULAR'){$type $this->type5;}
    else{
    $type $this->type1;


  • #2
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    They're exact equivalents, but the switch-case structure is less verbose.

    Personally, I use switches a lot. They just seem neater somehow, and certainly reduce the amount that needs to be typed. I also find them easier on the eye structurally.

    Another benefit of the switch structure is the possibility of catching everything you don't explicitly cater for with a "default". This, of course, is no different from a final "else {}", but the word "default", again for aesthetic purposes, but also semantically, seems very appealing to me.

    I've never done a benchtest for speed comparison, but I presume that if you're only checking a handful of things, the difference would be infinitesimal either way.

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I don't see much - if any - advantage switch has over if, even with lots of options in the statement. One big disadvantage with switch is the limited math functions you can use.

    PHP Code:
    switch(very little math functions)

    if (
    lots of math functions


    -----------------
    Leonard Whistler

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by Len Whistler View Post
    I don't see any advantage switch has over if, even with lots of options in the statement. One big disadvantage with switch is the limited math functions you can use with it:

    PHP Code:
    switch(very little math functions)

    if (
    lots of math functions
    That is fairly simple:
    PHP Code:
    switch ( TRUE )
    {
        case isset( 
    $this ) && == $this:
            
    // execute this
            
    break;
        case isset( 
    $that ) && == $that:
            
    // execute that
            
    break;
        default:
            
    // execute default

    switch() is also faster that if(), elseif(), else
    Last edited by kbluhm; 04-01-2010 at 01:43 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •