An inexperienced PHP question:
I've got a PHP script file that I need to include on different pages lots of times in lots of places.
I have the option of either breaking the included file down into several smaller files and include these on a as-needed basis... OR ... I could just keep it all together in a single PHP file.
I'm wondering if there's any performance impact of using a larger vs. smaller file for include() in this context? For example, is there any performance difference between a 200KB file and a 20KB file?
Thank you.
PHP Include Files. 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.
To get the file size, we will use filesize() function. The filesize() function returns the size of a file in bytes. This function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.
By default, PHP file upload size is set to maximum 2MB file on the server, but you can increase or decrease the maximum size of file upload using the PHP configuration file ( php. ini ), this file can be found in different locations on different Linux distributions.
The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.
There will be a difference, between a 200KB and a 20KB file... But you will probably not notice it : a 200KB file is not that big -- and you generally use a lot of files that are not "small", when you're building a big application.
There are two things that take time, when you're loading a .php
file :
As a sidnote : in a general situation, you'll gain a lot more time/cpu/resources optimizing your SQL queries, or adding some caching mecanism, than thinking about that kind of stuff.
Be careful with include_once()
(and also require_once()
), it is more expensive to run than include()
. Every time include_once()
is run, PHP does a lookup against an internal index of already included files before deciding whether to load the file or not. The more includes in the index, the slower this lookup is. Also when using include()
or include_once()
try to use absolute paths where possible as this is much speedier than relative paths because you are not forcing PHP to work out the absolute path for you. As ggiroux said, some form of caching like APC will reap massive rewards and render worrying about how many include calls you have irrelevant (largely) (unless you have some poorly written code).
EDIT--
Worrying about the above calls is only an issue once you to start have several thousand requires
or includes
in your codebase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With