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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:10:33+00:00 2026-06-11T13:10:33+00:00

What is quickest? To do a condition based on a variable outside the loop

  • 0

What is quickest? To do a condition based on a variable outside the loop inside a loop or outside, or does it even matter (does the compiler do it for you), or should you maybe use a completely different workaround?

Example #1 with the condition inside the loop (a single foreach):

$test = 2;  
foreach ($list as $listItem) {
    if ($test == 1) {
        $listItem .= " - one";
    } else if ($test == 2) {
        $listItem .= " - two";
    } else if ($test == 3) {
        $listItem .= " - three";
    }
}

Example #2 with the condition outside the loop (quite ugly with multiple foreach’s):

$test = 2;  
if ($test == 1) {
    foreach ($list as $listItem) {
        $listItem .= " - one";
    }
} else if ($test == 2) {
    foreach ($list as $listItem) {
        $listItem .= " - two";
    }
} else if ($test == 3) {
    foreach ($list as $listItem) {
        $listItem .= " - three";
    }
}
  • 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-11T13:10:34+00:00Added an answer on June 11, 2026 at 1:10 pm

    As you probably could’ve guessed, example #2 is quicker, and the compiler doesn’t do it for you.

    Overview

    condition inside or outside loop graph


    Performance tests

    Example #1, condition inside of loop:

    Time taken: 0.2501 seconds
    Time taken: 0.2336 seconds
    Time taken: 0.2335 seconds
    Time taken: 0.2319 seconds
    Time taken: 0.2337 seconds 
    Average:    0.2364 seconds
    

    And the code:

    <?php
        $list = array();
        for ($i = 0; $i < 1000000; $i++) {
            $list[] = md5(rand(0, 100000) * $i);
        }
        $a = microtime(true);
        $test = 2;
        foreach ($list as $listItem) {
            if ($test == 1) {
                $listItem .= " - one";
            } else if ($test == 2) {
                $listItem .= " - two";
            } else if ($test == 3) {
                $listItem .= " - three";
            }
        }
        echo "Time taken: " . number_format(microtime(true) - $a, 4) . " seconds";
    ?>
    

    Example #2, condition outside of loop:

    Time taken: 0.1424 seconds
    Time taken: 0.1426 seconds
    Time taken: 0.1364 seconds
    Time taken: 0.1348 seconds
    Time taken: 0.1347 seconds
    Average:    0.1382 seconds
    

    And the code:

    <?php
        $list = array();
        for ($i = 0; $i < 1000000; $i++) {
            $list[] = md5(rand(0, 100000) * $i);
        }
        $a = microtime(true);
        $test = 2;
        if ($test == 1) {
            foreach ($list as $listItem) {
                $listItem .= " - one";
            }
        } else if ($test == 2) {
            foreach ($list as $listItem) {
                $listItem .= " - two";
            }
        } else if ($test == 3) {
            foreach ($list as $listItem) {
                $listItem .= " - three";
            }
        }
        echo "Time taken: " . number_format(microtime(true) - $a, 4) . " seconds";
    ?>
    

    Alright, that’s a pretty obvious win for conditions outside of loops, but what if we try to compare with a boolean instead of an int?

    Example #3, condition inside of loop, but conditioning with a boolean instead:

    Time taken: 0.1845 seconds
    Time taken: 0.1821 seconds
    Time taken: 0.1745 seconds
    Time taken: 0.1777 seconds
    Time taken: 0.1767 seconds
    Average:    0.1791 seconds
    

    And the code:

    <?php
        $list = array();
        for ($i = 0; $i < 1000000; $i++) {
            $list[] = md5(rand(0, 100000) * $i);
        }
        $a = microtime(true);
        $test = 2;
        $result1 = ($test == 1);
        $result2 = ($test == 2);
        $result3 = ($test == 3);
        foreach ($list as $listItem) {
            if ($result1) {
                $listItem .= " - one";
            } else if ($result2) {
                $listItem .= " - two";
            } else if ($result3) {
                $listItem .= " - three";
            }
        }
        echo "Time taken: " . number_format(microtime(true) - $a, 4) . " seconds";
    ?>
    

    Interesting. What if we use a built in function such as array_walk?

    Example #4, array_walk:

    Time taken: 0.4950 seconds
    Time taken: 0.4946 seconds
    Time taken: 0.4947 seconds
    Time taken: 0.4937 seconds
    Time taken: 0.4918 seconds
    Average:    0.4940 seconds
    

    And the code:

    <?php
        function append_string($value, $suffix) {
            return $value . $suffix;
        }
    
        $list = array();
        for ($i = 0; $i < 1000000; $i++) {
            $list[] = md5(rand(0, 100000) * $i);
        }
        $a = microtime(true);
        $test = 2;
        if ($test == 1) {
            array_walk($list, "append_string", " - one");
        } else if ($test == 2) {
            array_walk($list, "append_string", " - two");
        } else if ($test == 3) {
            array_walk($list, "append_string", " - three");
        }
        echo "Time taken: " . number_format(microtime(true) - $a, 4) . " seconds";
    ?>
    

    What?! You might think an inbuilt function would abstract it over a C or C++ function, and it might very well, however the problem is that the function calls make this method very very slow.


    Pros/cons

    Example #1 pros

    • You don’t have to declare the loop multiple times (which might be annoying if you have several conditions)

    Example #1 cons

    • It is the slowest one of all the first 3 tests (71% slower than example #2 and 32% slower than example #3)

    Example #2 pros

    • The very quickest one of them all

    Example #2 cons

    • It requires you to declare the loop several times (as many times as the amount of conditions you have)

    Example #3 pros

    • You don’t have to declare the loop multiple times, just like in example #1
    • It is quicker than #example 1

    Example #3 cons

    • It is still slower than #example 2 (about 30%)
    • It looks more messy

    Example #4 pros

    • The syntax looks pretty and easy

    Example #4 cons

    • It is the slowest one of the lot (about 100% slower than the second slowest (example #1)). Possibly due to it having to call a function 1,000,000 times.
    • You’re out of the global scope and will have to global all variables from the global scope.

    Conclusion

    The PHP compiler doesn’t do it for you, and if you want performance you should go for example #2, however this test is made with an array that has a million entries. Chances are that your array isn’t going to have that many entries, and therefore you might go with example #1 or example #3 instead. Oh, and do not go for example #4.

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

Sidebar

Related Questions

Whats the easiest and quickest way to loop through an array of numbers and
Which is the quickest way in Java to sort a list List<MyObject> myObjectList based
Which is the quickest way to view the value of a NSCFString variable in
What is the quickest way to read a text file into a string variable?
Which is the quickest way to check the presence of Struts runtime from a
What is the quickest way to jump to a particular symbol/selector/class in Xcode? (I'm
What's the quickest, easiest way to draw text in standard OGL ??
What is the quickest most efficient way to search text for words using non-casesensitive
What is the quickest way to find the number of elements in a static
What would be the quickest way to find the position of a member in

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.