views:

226

answers:

7

I am developing an application where I have a requirement that I have to include multiple files, as follows:

File1.php will include File2.php

File2.php will include File3.php

File3.php will include File4.php

Now, what will be the best and optimized way of doing these. I tried with include() but I think its slowing down my server as my server load graphs goes approx 3-5 times higher than normal server load.

These all files are heave means arround 50 KB for each file. So can anyone please suggest me what will be the best option to include these files so that it will not affect the speed of the site,

Which will be best?

require()

require_once()

include()

include_once()

OR if anyone has some other option then please tell me,

Thanks

A: 

Your answer will depend on your need, mostly. Points to keep in mind are:

  1. include is faster than require.
  2. if a file is "included" but is not found, it does not result in a fatal error. The script will continue running.
  3. if a file is "requried" but is not found, the script execution stops instantly.

However, a side note: if your application is including file4 in file3, file3 in file2 and file2 in file1, then I'd say you should reconsider your file organisation. I'm sure you can come up with a better way of organising your code into files.

ADDITION: The speed difference between include and require is pretty marginal. However, require_once() is significantly heavier than require(). Likewise for include_once() and include().

cheers,

jrh

Here Be Wolves
Do you have a source for the speed differences between include and require? I would like to see that tested somewhere.
Jergason
Either way, the speed difference will be a couple microseconds; nothing worth fretting over.
musicfreak
Well I don't have the source code. But I distinctly remember reading it in some documentation. But yea, the couple microseconds difference is hardly worth fretting over.
Here Be Wolves
include() and require() both execute the same code. Only in case of an (file open) error there is a if(type==require) { message, bailout } else { message } ( see zend/zend_language_scanner.l, compile_file() )
VolkerK
+3  A: 

The difference between include() and require() is whether you get a warning or a fatal error (respectively). The PHP docs don't mention any performance difference.

Using the _once() variants is useful if you want to avoid duplicating the contents of the included files. From the PHP docs for include_once:

include_once() may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

Jimmy
+2  A: 

You should reorganize your files. Have a "common.php" file that includes all the needed files. You shouldn't have a chain like that.

I sincerely doubt a couple of includes are slowing down your server. Most web applications have tens, maybe even hundreds of PHP files. There is no way 3 extra files will slow down your application that much.

Also, the speed difference between the functions you mentioned is negligible, although you should use include_once/require_once instead of their counterparts, to make sure you don't include the same file multiple times.

musicfreak
I think file organization is OK, caz if I'll use common.php to include other files then, File1 will include common.php and the common.php will get the reuiqred file. Almost same I am already doing ....
Prashant
Everywhere on net I have read that "include_once/require_once" are slower than "include/require" and reuire is slower than include ?? I am not getting exactly who is write and who is wrong ???
Prashant
@Parshant: Yeah, but my way will be way more organized and it will be more maintainable in the future. Honestly, stop worrying about the very small speed difference between the functions. 0.000002 seconds slower won't do you any harm. ;)
musicfreak
@Prashant: If in doubt, measure it yourself. In the real world, the speed difference between those 4 functions is negligible - the real performance bottlenecks are elsewhere and several orders of magnitude more serious (again, performance measuring is a necessity here).
Piskvor
A: 

I prefer splitting everything into as many files as it’s convenient for me to develop, but then flatten it all into one file before uploading it to server.

PHP string and regex functions make it super easy to write a script that will replace any include 'somefile.php'; with actual contents of that file (stripping < ? ? > if necessary).

Ilya Birman
Have you measured the speedup from doing that? I'd like to know if that has any visible effect on performance.
musicfreak
can you please dmeonstrate it to me, by giving any example of this kind ???
Prashant
A: 

Definitely choose include() or require() over the '*_once()' variants. You probably need some sort of opcode cache to improve your performance.

too much php
+1  A: 

Jusn a note to know. In one of my projects I had to implement a kind of factory for lots of classes (about 50). I created a file for each class and including them was dramatically slow. I decided to join them into one file. This incereased performance very appreciable.

I think it is related to file system slowdowns when there are many entries... Not sure why it's so, but fact - include of 1 big file much faster then 50 small.

Jet
True, but the difference between 1 file and 4 will be much smaller.
musicfreak
Also true, but when you use 4 instead of 1 and do this 10 times in a project... 4*10 = 40 instead of 10...Well, this is just a note and, posiibly, offtopic. I only mean "keep this issue in mind".
Jet
A: 

I would suggest using xdebug PHP extention to benchmark and profile your script. It will tell you how long each function took to execute. You can find more information on how to install it and what it can do here: http://www.xdebug.org/

Benjamin Ortuzar