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 7989353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:43:52+00:00 2026-06-04T12:43:52+00:00

Which is the better and fastest methods : if or switch ? if(x==1){ echo

  • 0

Which is the better and fastest methods : if or switch ?

if(x==1){
  echo "hi";
} else if (x==2){
  echo "bye";
}

switch(x){
  case 1
    ...
  break;
  default;
} 
  • 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-06-04T12:43:54+00:00Added an answer on June 4, 2026 at 12:43 pm

    Your first example is simply wrong. You need elseif instead of just else.

    If you use if..elseif... or switch is mainly a matter of preference. The performance is the same.

    However, if all your conditions are of the type x == value with x being the same in every condition, switch usually makes sense. I’d also only use switch if there are more than e.g. two conditions.

    A case where switch actually gives you a performance advantage is if the variable part is a function call:

    switch(some_func()) {
        case 1: ... break;
        case 2: ... break;
    }
    

    Then some_func() is only called once while with

    if(some_func() == 1) {}
    elseif(some_func() == 2) {}
    

    it would be called twice – including possible side-effects of the function call happening twice. However, you could always use $res = some_func(); and then use $res in your if conditions – so you can avoid this problem alltogether.

    A case where you cannot use switch at all is when you have more complex conditions – switch only works for x == y with y being a constant value.

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

Sidebar

Related Questions

Possible Duplicate: array_push() vs. $array[] = … Which is fastest? Is it better to
Possible Duplicate: array_push() vs. $array[] = … Which is fastest? Is it better to
Which constitutes better object oriented design? Class User { id {get;set} } Class Office
Which is better? if not var: var = get_var() (or) var = var or
Which is better way to use jQuery() ? I've heard that jQuery(element) doesn't ruin
Which is better in respect to performance and memory utilization? // + Operator oMessage.Subject
Which has better execution performance: CSS3 animations, or jQuery animations? How do they affect
Is there any considerations to determine which is better practice for creating an object
I don't want to make this into a which is better... MVC or WebForms
Between interfaces and enums, which is better for declaring constants? Why is it so?

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.