Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 983515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:52:11+00:00 2026-05-16T04:52:11+00:00

Possible Duplicate: is “else if” faster than “switch() case” ? I’ve encountered a lot

  • 0

Possible Duplicate:
is “else if” faster than “switch() case” ?

I’ve encountered a lot of situations lately where I have very simple conditionals and need to branch application flow. The “simplest” means of accomplishing what I’m doing is just a plain old if/elseif statement:

if($value == "foo") {
    // ...
} elseif($value == "bar") {
    // ...
} elseif($value == "asdf" || $value == "qwerty") {
    // ...
}

…but I’m also considering something like:

switch($value) {
    case "foo":
        // ...
        break;
    case "bar":
        // ...
        break;
    case "qwer":
    case "asdf":
        // ...
}

This seems a little less readable, but perhaps it’s more performant? However, when there are more and more “or” expressions in the conditional, it seems that the switch statement is much more readable and useful:

switch($value) {
    case "foo":
        // ...
        break;
    case "bar":
    case "baz":
    case "sup":
        // ...
        break;
    case "abc":
    case "def":
    case "ghi":
        // ...
        break;
    case "qwer":
    case "asdf":
        // ...
}

I’ve also seen options where code flow is branched using arrays and functions:

function branch_xyz() {/* ... */}
function branch_abc() {/* ... */}
function branch_def() {/* ... */}

$branches = array(
    "xyz"=>"branch_xyz",
    "abc"=>"branch_abc",
    "def"=>"branch_def"
);
if(isset($branches[$value])) {
    $fname = $branches[$value];
    $fname();
}

This last option also presumably has the benefit of being distributable across multiple files, though it is pretty ugly.

Which do you feel has the most advantages with the fewest tradeoffs in terms of performance, readability, and ease of use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:52:12+00:00Added an answer on May 16, 2026 at 4:52 am

    Personally, I find the switch a lot more readable. Here’s the reason:

    if ($foo == 'bar') {
    } elseif ($foo == 'baz') {
    } elseif ($foo == 'buz') {
    } elseif ($fou == 'haz') {
    }
    

    Condensed like that, you can easily see the trip up (be it a typo, or a honest difference). But with a switch, you know implicitly what was meant:

    switch ($foo) {
        case 'bar': 
            break;
        case 'baz': 
            break;
        case 'buz': 
            break;
        case 'haz': 
            break;
    }
    

    Plus, which is easier to read:

    if ($foo == 'bar' || $foo == 'baz' || $foo == 'bat' || $foo == 'buz') {
    }
    

    or

    case 'bar':
    case 'baz':
    case 'bat':
    case 'buz':
        break;
    

    From a performance standpoint… Well, don’t worry about the performance. Unless you’re doing a few thousand of them inside of a tight loop, you won’t even be able to tell the difference (the difference will likely be in the micro-second range, if not lower).

    Go for the method that you find the most readable. That’s the important part. Don’t try to micro-optimize. Remember, Premature Optimization Is The Root Of All Evil…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.