-2

So if I have the following code:

<?php
function someFunction()
{
    //lots of lines of code
}

if ($someBooleanVariable)
{
    //some random code
}
else
{
    someFunction();
}

?>

Question 1: Am I correct to assume that the server will first load the whole function, and then possibly not even use it?

Question 2: If that were the case, wouldn't it often be more efficient to place the function code inside the brackets "{}" after the else in the previous code? (assuming that the code would only run once and wouldn't need to be used anywhere else).

Since the code would only load and run if the "$someBooleanVariable" was FALSE, I assume that the server would be less used if the code wasn't in the function but rather inside the brackets after the "else". However I am basing that on the assumption that the server is loading the function code automatically every time since it is at the top of the page, which might be the root of the question.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • You likely spent more time typing this question than the server would save in a thousand years of operation by optimizing this behavior. – ceejayoz Nov 13 '15 at 01:36

1 Answers1

1

PHP has to parse the WHOLE file, creates some bytecode from it that represents the source code, and then executes that bytecode.

It does not matter where you place your function - it will always be parsed and made into bytecode.

Once it is bytecode, the execution path may differ, i.e. the function may not be called, but I fail to see that this makes anything but minor differences.

All in all, you are asking about microoptimizations. If you can write code that clearly proves that one approach is better than the other, and that the speed differences are significant (i.e. they are more than 10% faster), and that the code readability is not significantly worse, you have a valid case.

I don't think you have a valid case here. The function will get parsed independently of where it is placed, and executed only when the code path leads there.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Worth noting that putting the function definition inside the else{} will mean that it won't be registered for use by the code until that point. That might save an immeasurable amount of processing if it is never triggered. – rjdown Nov 13 '15 at 01:32
  • 1
    It is for good reasons to treat function declarations different that the actual code path - it's making the code more clear and understandable for the human reader, and this is paramount. Everything else may be optimized by the engine. – Sven Nov 13 '15 at 01:36
  • Absolutely. I'd go a step further and use static methods in helper classes, instead of separate functions all over the place. Let the autoloader handle it :) – rjdown Nov 13 '15 at 01:40
  • Avoid static methods. – Sven Nov 13 '15 at 01:40
  • Bit of a sweeping statement... for shame. – rjdown Nov 13 '15 at 01:57
  • K thanks for that, your answer gave me the background information that I needed – Webeng Nov 13 '15 at 02:36