If I use too many includes
on my page will it slow down or something like that?
If I use too many includes
on my page will it slow down or something like that?
Include does not slow down the page, but rather the contents of the file that will be included.
The performance depends on the contents of your file that will be included, for example, many FOR cycles make the script slower, a database connection with a query badly built, etc. ..
The best thing would be to look at how to improve the performance of your php script, best development practices, etc ...
Only the include function will not affect performance. Now if you compare a require () with an include () yes, require > is slower than include .
Use absolute paths when calling files, so php will not need to parse include_path
.
Here are some examples of good practice for you that are starting:
When you make an include / require you obviously intend to include / require a resource from another external file, whether it is local or remote. This file (s) is stored on a disk which, in order to be read, depends on the file system itself, which is slow by definition. / p>
With this little opinion you already know that yes , using too many includes / requires is bad for performance.
But there is another very significant factor against multi-file fragmentation, which is the interpretation of written code. If you include 15 files in a single file to be accessed by a given request, the interpreter will "read" and interpret all tokens and parse the syntax of 15 files before server to send a response as output to the browser.
This not only increases the time between one request and another, it also makes any debugging error a Herculean task for the developer.
There is no difference between typing, for example, 15 lines of code in a file or playing 5 of those lines to another PHP file and giving an include in it.
In some cases you will want to use include_once
to ensure that the same file is not included 2 (or more) times ( see ).
An include can disrupt perfomance because you can make includes of unnecessary files at times of code you will not even use.
You should study your system type well, and if you have variables in common with solid values, use them as constants (if the value is immutable).
But if you have variables / methods in common with mutable values, create classes and inherit (or extend) them so you can simply pass on the values that will be handled by reference and / or parameter, but this will depend a lot the type of system, and the type of architecture you are adopting for development.
Always look at design patterns and code management, so over time you will always be making more robust and less volu- ble and cleaner code that throughout your career you will have a good ready code library for use in later projects.