Switch Case vs. Conditional?

I found this post: Switch case with three parameters? and I was considering using switch case passing multiple parameters like this:

switch (array($var1, $var2, $var3)) {
case array(true, false, false):
    echo "hello";
    break;
}

There seems to be some question as to whether this is the most efficient way of doing this. The mood seems to be that an if conditional is more appropriate. But, when I look at the conditional I'm writing, I'm not sure? For example this feels messy (note that I've removed about 6 other conditionals to keep from boring you):

if ( 
  csz == "google" || 
  csz == "bing" || 
  csz == "yahoo" || 
  csz == "dogpile" || 
  csz == "millionshort" && 
  cs == '' ) { 
    $("li.phone").replaceWith('<li class="phone">' + phoneNaturalSearch + '</li>');
}
else if ( 
  csz == "facebook" && 
  cs == '' ) { 
    $("li.phone").replaceWith('<li class="phone">' + phoneFacebook + '</li>');
}
else if ( 
  csz == "google-plus" || 
  csz == "plus" ) { 
    $("li.phone").replaceWith('<li class="phone">' + phoneGooglePlus + '</li>');
}
// Snipped out other conditionals
else { 
    $("li.phone").replaceWith('<li class="phone">' + phoneDefault + '</li>');
}

Would switch with multiple parameters be more efficient here, or would I experience a performance hit? I guess I should write the code and see if it's actually less messy, but I thought I'd bounce it off of the gurus first.

Answers


I feel if you have these many conditions I prefer to use switch statements which makes your code cleaner and easier to understand.. You can omit the break's for the conditions that fall for this particular category...

var phoneType= '';

switch(csz){ 
  case "google" : 
  case  "bing" : 
  case "yahoo" :
  case "dogpile"  : 
  case "millionshort" : 
    if (cs==''){ 
        phoneType = phoneNaturalSearch ;
        break;
    }
    else {
        goto case "facebook";
    }
  case "facebook" :
    if (cs==''){ 
        phoneType = phoneFacebook  ;
        break;
    }
    else {
        goto case "google-plus";
    }
  case "google-plus" :
  case "plus"  : 
    phoneType = phoneGooglePlus ;
    break;
  default : 
    phoneType = phoneDefault  ;
    break;
}

$("li.phone").replaceWith('<li class="phone">' + phoneType + '</li>');

Need Your Help

android notification launches same activity twice

android android-notifications

When I click the notification on the status bar it launches an activity but the behavior is strange. If my app is in foreground and I click the notification the notification intent is fired once. I...

Compile c++ code using Magick++ and openMPI

c++ imagemagick openmpi magick++

I'm trying to compile my C++ code using Magick++ library to manipulate images in a distributed way using openMPI and I get some errors when I try to compile it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.