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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:08:47+00:00 2026-05-25T06:08:47+00:00

In PHP, is there a fastest/proper way of ordering if/else if statements? For some

  • 0

In PHP, is there a fastest/proper way of ordering if/else if statements? For some reason, in my head, I like to think that the first if statement should be the anticipated “most popular” met condition, followed by the 2nd, etc. But, does it really matter? Is there is a speed or processing time affected if the 2nd condition is the most popular choice (meaning the system must always read the first condition)

Ex:

if ("This is the most chosen condition" == $conditions)
{

}
else if ("This is the second most chosen condition" == $conditions)
{

}
else if ("This is the third most chosen condition" == $conditions)
{

}
  • 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-25T06:08:48+00:00Added an answer on May 25, 2026 at 6:08 am

    Speedwise, it won’t make a difference… Not a noticeable one… The order does count (it’s sensibly faster putting the most used condition first), however it doesn’t count for much. Choose the order which provides the best readability to those modifying and maintaining your code. They’ll thank you later.

    EDIT:
    Also, consider this:

    My function will return with a 25% chance.
    I prefer writing:

    if ( $chance25 )
        return;
    else if ( $chance40 )
        doSomething();
    else if ( $chance30 )
        doSomethingElse();
    else if ( $chance5 )
        doSomethingElse2();
    

    rather than:

    if ( $chance40 )
        doSomething();
    else if ( $chance30 )
        doSomethingElse();
    else if ( $chance25 )
        return;
    else if ( $chance5 )
        doSomethingElse2();
    

    It’s just nicer ordering by functionality…

    EDIT2:

    One size does not fit all. If your conditions are methods returning booleans, order them by how fast the method runs combined with the chance. I guess there’s not really one good answer, you need to adapt. For example, if my $chance25 was replaced by a method reallySlowMethodDoNotUseUnlessYouReallyHaveTo(), I would surely check it last. 😀

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

Sidebar

Related Questions

I heard that in PHP there are some alternatives for the functions substr() and
I am trying some things with JSON and PHP and there is something that
In PHP there are functions like unset() that support any number of parameter we
Possible Duplicate: Fastest way to retrieve a <title> in PHP Suppose there is a
What is the fastest way to store config data in PHP so that it
What's the fastest (best performing) way in PHP to transform a (My)SQL result like:
I seem to remember that in PHP there is a way to pass an
I was simply going through the tutorial of PHP there I found that we
In php is there a way to write binary data to the response stream,
IN PHP: Is there a way for the user to fake a session variable?

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.