7

This question has to do with the internal mechanics of the PHP engine. I'm asking it in an effort to understand the file include process.

Say, you got a 20,000 lines include file ( something like "functions_library.php" ) that gets included on all your php scripts.

Does PHP check/verify if that include file is syntactically correct every single time one of your php scripts load that file? Does this process happen at each page load over and over and over again?

Or...

Does PHP pay attention to the file's last modification date? If it turns out that there were no changes to it since the last check, does it simply ignore the checking?

1
  • 1
    +1 Good question. Would like to know this as well. Commented Jul 26, 2012 at 14:05

3 Answers 3

4

On a default installation, the file will be parsed every single time. However, any production installation of PHP is recommended to have a bytecode cache, such as APC or many others. When bytecode cache is used, the script is parsed the first time and the interpreted code will be stored in memory.

Different configurations may alter how often file modifications are checked. Under some configurations, for very high performance, manual flushing or restarting the web server may be required.

Sign up to request clarification or add additional context in comments.

4 Comments

How about 10000 web users accessing the same php file ( the home page for example ) all within the same 1 second. Does the server still process the darn 20000 line include file 10000 times for each user or does it have a better handling at this?
to begin with, 20,000 lines of code is nothing. If you don't have bytecode cache enabled, it will do a lot of unnecessary rework. As for 10,000 users within the same second, that is a lot, and your single server will die.
well, louis, that was not my point. 10000 users surely bring a single server down. I should have picked a more realistic number, how about 20. Would the PHP interprets this file 20 times within that second for each and every user or does it serve the interpreted code from the server's cache? I'm not familiar with the bytecode cache.
The bytecode cache is a compiled version of the code. So there won't be any validation or parsing required. The code still needs to run for every request, which is the actual interpretation. You can write your code to cache some slower portions, or use a cache server ahead like Varnish and set appropriate headers. However this is a very wide topic.
1

If you include that file, PHP will need to interpret it every time.

1 Comment

That makes some sense, since the key word here is "interpret" and not "compile."
0

PHP will re-interpret the file every time you call include().

This can be tested visually with the use of variables. If you have something like this:

otherScript.php:
   <?php echo $foo; ?>

script.php:
    <?php
    $foo = 1;
    include("otherScript.php");
    $foo = 2;
    include("otherScript.php");
    ?>

The output will be:

12

This is a poor example, but it should demonstrate the PHP will need to include the contents of the file each time. It is a pretty good guess to state that the PHP interpreter won't keep a copy of the included file in memory for each reference - there can be hundreds of includes per application.

To deal with this specific ideal, however, they provide a method include_once() that will, as the name implies, only include the file one time and prevent it from being included any additional times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.