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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:55:17+00:00 2026-05-27T08:55:17+00:00

Considering this class Class 1 class myclass { public static function myfunction($condition, $string) {

  • 0

Considering this class

Class 1

class myclass {
    public static function myfunction($condition, $string)
    {
        if ($condition) {
            // A lot of code here
            // This is just a stupid example
            echo $string;
        }
    }
}

Class 2

class myclass {
    public static function myfunction($condition, $string)
    {
        // A lot of code here
        // This is just a stupid example
        echo $string;
    }
}

and the following files:

File 1

myclass::myfunction(($i > 1), '$i is > of 1');
myclass::myfunction(($i > 2), '$i is > of 2');
myclass::myfunction(($i > 3), '$i is > of 3');
myclass::myfunction(($i > 4), '$i is > of 4');
myclass::myfunction(($i > 5), '$i is > of 5');
...
myclass::myfunction(($i > 50), '$i is > of 50'); // this is the amount of functions calls in my project more or less...

File 2

if ($i > 1) { myclass::myfunction('$i is > of 1'); }
if ($i > 2) { myclass::myfunction('$i is > of 2'); }
if ($i > 3) { myclass::myfunction('$i is > of 3'); }
if ($i > 4) { myclass::myfunction('$i is > of 4'); }
if ($i > 5) { myclass::myfunction('$i is > of 5'); }
...
if ($i > 50) { myclass::myfunction('$i is > of 50'); }

Which file will run faster (considering both 2 different classes) on the same work base?
Does PHP cache classes method request or just keep looking for the class, the method and then execute it? Does it change that much if I keep the condition inside the method (so the method will be executed)?

  • 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-27T08:55:17+00:00Added an answer on May 27, 2026 at 8:55 am

    I would guess that case 2 is technically faster because you don’t have to pass the expression and the function isn’t even called if the expression is false (thanks ajreal!). Even in the worst case situation, where the expression is always false, the second would be faster (in C++ at least) unless the compiler optimized passing the expression out.

    However, they are both theoretically the same running time (BigO-wise) and if you are having performance issues, this isn’t the cause of them. In other words, the difference is negligible. Premature optimization is the root of all evil.

    If you still insist on thinking this is significant, it shouldn’t be hard to benchmark yourself. Try running each ten thousand times with random expressions/strings and compare the time difference.

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

Sidebar

Related Questions

considering this example: public static void main(final String[] args) { final List<String> myList =
Considering this basic code snippet: <? class mcClassington { var $mcProperty='default'; function mcDoSomething() {
Considering this code : interface IRepository<T> { void Save(); } class Repository<T> { public
Considering this class public class XQueries { public IQueryable Query1() { using (XEntities context
So if i have something like this: public class TestClass { public static void
Considering this class: public class Foo { public Int32 MyField; } I guess the
Considering this code, can I be absolutely sure that the finally block always executes,
I have this small code library that I'm considering releasing into Open Source. I
I apologize if this code looks a bit like a mess (considering the length);
I'm considering the approaches to write class MyClass { @Get @Set protected int aValue;

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.