Does reading include files slow down php script load?

It is very common to use include files. I think it is overused to keep the codes tidy without considering performance. For several includes, disk should read the files, and as we extremely use disk, it can be a slow process. However, this is not the main slow process or the rate-limiting process, as loading the file with file_get_contents is few times faster.

I think this is the reason that major websites put javascripts within the html file rather than loading them by file. Alternatively, it can be a good idea to split a large JS file into several small JS files, as parallel http requests can load the entire JS codes faster. But this is different from php files, as php script reads include files one by one during the process.

  1. Please comment how much serious this problem can be? Imagine a webpage is loaded in 0.60s, can include of 10 php files turn it to 0.70s?

  2. Although this effect should be negligible, I want to know if there are approaches to speed up this process. I do not mean php caching like APC.

P.S. This question is not for practical application (a typical case), but theoretical considerations in general.

include and its ilk is a necessity. It is similar to import in Java and python in that it is used for class and function definitions. include should be extremely fast, but using it will delay script execution compared to if it was not there. include is totally different from file_get_contents(). The latter is a function rather than a construct and returns a string. include will actually execute the code of the included file.

Your statement about splitting JS files is incorrect as script downloads from the same domain block parallel downloads and it's generally recommended to have as few includes as possible in general.

I highly doubt that having multiple includes, assuming all are necessary, is going to slow down the performance of your page. If you are having performance problems, look elsewhere.

If you want to speed up php, look into using a php compiler.

include - Manual, A blank line in a CSV file will be returned as an array comprising a single null fgetcsv() returns NULL if an invalid handle is supplied or FALSE on other errors, including end of file. Example #1 Read and print the entire contents of a CSV file delimiters and line endings prior to importing the file into MySQL using LOAD  I have added two includes to load a header and footer via PHP and since I have added these includes the page loads incredibly slow. Is there an issue with page loading times when adding includes

Consider this:

for ($i=0; $i<100000; $i++) {

// nothing here

index.php takes ~115 seconds (for me) to process 100,000 iterations while including somefile.php, even though somefile.php has nothing in it.


for ($i=0; $i<100000; $i++) {
    // no file included this time

index.php now takes 0.002 seconds to complete without an include() construct.

for ($i=0; $i<100000; $i++) {
    echo $i .'<br/>';

index.php takes 0.02 seconds to echo 100,000 iterations of $i.

Of course, this is a pretty extreme example due to the large number of iterations, but it does show that by simply including an include construct, script execution times can be delayed quite exponentially. Consider this the next time you write a process with large numbers of iterations, ie. reading/writing large XML files, etc. It's best to keep your code inline, even if that means it's less "manageable". 'Cause not only are you adding ~115 seconds (~2 minutes) to script execution time at every ~100,000 iterations simply by including an include(), but consider if that include() (somefile.php) had processes of its own to execute. My example is simply adding an include() construct.. the included file contained nothing.

Now, including files here and there for a webpage, the times would be negligible. I was only pointing out that the include() construct does require extra processing regardless of its contents.

fgetcsv - Manual, imagejpeg() creates a JPEG file from the given image . Here's a complete solution to READ any image (gif jpg png) from the FILESYSTEM, SCALE it to a max  But PHP itself does not provide a way to find out slowly executing scripts. Slow scripts are not the ones which break your site but they slow-down everything. Using FPM, we can have a slow_log for all such scripts. Let’s see how to use these logs to debug PHP scripts. Also, we will see how PHP’s error_log gets diverted if it is running

Yes, it does. The libraries you used to use will bring performance penalty due to a lot of includes underneath. The best approach to improve performance is:

  1. Put together all included files in a single one
  2. Use accelerator

It can speed up your solution by 22 times. Read more Here

imagejpeg - Manual, scandir — List files and directories inside the specified path Multiple dirs, with specified extensions, include sub-dir files whose file system ignores the case of file names when it does its own sorting. If you have a folder with many files and/​or subfolders, doing a recursive scandir will likely either slow down your  The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website. It is possible to insert the content of one PHP file into another PHP

PHP has to parse the code no matter if it is in the main php file or an include. Putting it in an include probably make no difference. Disk speed makes no difference either since it will be cached after the first time.

scandir - Manual, If you just want to dump the contents of a file to the output buffer, without first Otherwise, fpassthru() returns the number of characters read from handle and getting corrupted, one of the scripts included/required in your download script or page Other links can now be clicked and loaded whilst a big file is being passed  When including a file using its name directly without specifying we are talking about the current working directory, i.e. saying (include "file") instead of ( include "./file") . PHP will search first in the current working directory (given by getcwd() ) , then next searches for it in the directory of the script being executed (given by __dir__).

fpassthru - Manual, Returns the size of the file in bytes, or FALSE (and generates an error of level As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Now it returns a two characters file size which is a bit more convenient to read. A simple and improved function to get the folder size including subfolders​: Yet, all of this time, effort and money needed to maximize holiday sales could be in vain if increased site traffic over the holidays causes a website to slow down or even go down. It’s no secret that performance matters to users. Site speed directly affects bounce rates, conversion rates, revenue,

filesize - Manual, This can include HTML, CSS, and JavaScript files, as well as Google Fonts. If your scripts load synchronously, they load one at a time, in the order they need to interact with a database to be “built” with PHP before they are delivered. They're often very large files, which can slow down page load times. A large volume of unoptimized images is usually the most common reason behind website slowness. High-resolution images can consume lots of bandwidth while loading. Uploading larger sized images and then scaling them down can unnecessarily increase the size of your web page – causing your website to load slowly.

20 Ways to Speed Up Your Website – and Improve Conversion by 7%, General tips; Tools on your computer; PHP and CGI in links and includes; WordPress Keep file sizes small. spyware and other malicious software, and also can be tougher to load for the web browser, Some programs, e.g. Joomla, will slow down even if they are not activated, only installed. Read more / order In this the above test, I fill in the two input fields and click submit. The data is written to the data.php file and when I refresh, I can see the updated data in the form. So in this case, data isn’t being written too slow, it’s actually written as the script tells it to be.

Frequently asked questions, This process involves a lot of steps, and it can really slow down your website when that using colored visuals makes people 80% more likely to read your content. The downside is that it's a larger file size, so it takes longer to load. These static files include unchanging files such as images, CSS, and  Because some scripts may take an excessive amount of time to run, Internet Explorer prompts the user to decide whether to continue running the slow script. Some tests and benchmarks may use scripts that take a long time to run. Therefore, you may want to increase the amount of time that passes before the message box appears. In Internet

  • Using include should be extremely fast, and even faster if you enable APC. Your chasing the wrong end of the stick here.
  • Also, parallel request are normally slower due to the connection handling overhead. Simply combine all JS files and put them in your document root, then let your web server (Apache) handle the caching.
  • It could be very serious... if you're using machines from 70's :p How "fast" do you need the PHP code to run? Does 0.1s matter? How bad is it compared to highly reusable, flexible and extensible architecture?
  • Combining JS means less requests are needed, thus allowing other files to fill up the parallel download queue (I think the limit is generally 4), but there is an advantage to "chunking" (only combining up to a limit, eg 20-100KiB): the user can run some code before all of it has been downloaded. If someone is on a slow connection and your website is entirely unusable without a couple crucial JS files (which is a bad design in my mind), they'll appreciate being able to do something sooner.
  • Thanks for your informative answer. I know the difference of include and file_get_contents. By that comparison, I meant reading file from disk can be faster; then, the rate-limiting step is php processing of include.
  • +1 for quoting php compiler. I was always in doubt about php compiler (due to opposite opinions). Now I must try it :)
  • No, the file is cached by the OS (just the regular disk cache, PHP files fit in it very easily). APC would cache the result of parsing the file.