views:

94

answers:

6

My question is whether or not using multiple PHP includes() is a bad idea. The only reason I'm asking is because I always hear having too many stylesheets or scripts on a site creates more HTTP requests and slows page loading. I was wondering the same about PHP.

+5  A: 

The use of includes helps with code organization, and is no hindrance in itself. If you're loading up a bunch of things you don't need, that will slow things down -- but that's another problem. Clarification: As you include pages, be aware what you're adding to the load; don't carelessly include unneeded resources.

Smandoli
A: 

It really depends on what you want to do, I mean if you have a piece of code that is used all the time it is really convenient to include it instead of copying and pasting all the time and that will make your code more clear and not slower, but if you include all the functions or classes you have written in your files without using them of course thats not a good practice...I would suggest using a framework (like codeigniter or something else you find convinient) because it really helps clearing this things out... good luck!

rabidmachine9
+8  A: 

The detailed answer:

Every CSS or JS file referenced in a web page is actually fetched over the network by the browser, which involves often 100s of milliseconds or more of network latency. Requests to the same server are (by convention, though not mandated) serialized one or two at a time, so these delays stack up.

PHP include files, on the other hand, are all processed on the server itself. Instead of 100s of milliseconds, the local disk access will be 10s of milliseconds or less, and if cached, will be direct memory accesses which is even faster.

If you use something like http://eaccelerator.net/ or http://php.net/manual/en/book.apc.php then your PHP code will all be precompiled on the server once, and it doesn't even matter if you're including files or dumping them all in one place.

The short answer:

Don't worry about it. 99 times out of 100 with this issue, the benefits of better code organization outweigh the performance increases.

dkamins
I thought there must be some integral difference between CSS, etc. and PHP pages. Thanks much.
Smandoli
The best part about your answer was explaining how JS and CSS worked. I wish more people would go indepth in their answers. Thanks! :DBtw, I stumbled upon this project and is this an tool to precompile code like the tools above?
NessDan
When opcode cache is used, more included (=smaller files), is a bit faster than everything-in-one-huge-file. But you shouldn't worry about this. Focus on code readability instead.
Jacco
A: 

The only reason I'm asking is because I always hear having too many stylesheets or scripts on a site creates more HTTP requests and slows page loading. I was wondering the same about PHP.

Do notice that an HTTP request is several orders of magnitude slower that a PHP include.

Several HTTP requests -> Client has to request and accept over the wire several files
Several PHP includes -> Client has to request and accept only one file

The includes, obviously, will have a server penalty. But by your question... don't sweat it. Only on really large-scale PHP projects will you face such problems.

Frankie
+1  A: 

As already said, the use of multiple PHP includes helps to keep your code organized, so it is not a bad idea. It will become a problem when too many includes are used, because the web server will have to perform an I/O operetation for each include you have.

If you have a large web application, you can boost it using a PHP accelerator, which caches data and compiled code from the PHP bytecode compiler in shared memory. If you have lots of PHP includes in a specific file they will be performed just once. Further calls to that file will hit the cache, so no PHP require will be performed.

Dovyski
A: 

PHP can detect the inclued file as you can use the include_once() function instead of include().

Dat Nguyen