Possible Duplicate:
Why is require_once so bad to use?
I've read somewhere that the include_once
and require_once
statements in PHP were slower than their non-once counterparts. Is this a significant slowdown? Has there been any testing or studies of this, and has it changed in recent versions of PHP?
They are all ways of including files. Require means it needs it. Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue.
include_once ¶ This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns true . As the name suggests, the file will be included just once.
You should use require_once when you do not need the file's contents included multiple times. It is also helpful if you are working on a script and are unsure if the file has already been included and you do not want it included twice.
The only difference between the two is that require and its sister require_once throw a fatal error if the file is not found, whereas include and include_once only show a warning and continue to load the rest of the page.
The speed increase is minimal and comes as a reference check is conducted to prevent code duplication. The 'once' appendage is a preventative measure against the same code being executed/included twice..this performing this check comes at a minor speed cost.
If there is ever an instance where you are using _once
look into why it is the case, is you code really built in the most efficient way? It is often better to remove the need to rely on _once
and produce better code (easier said than done!).
See:
http://forums.digitalpoint.com/showthread.php?t=1693837
http://www.phpbb.com/community/viewtopic.php?f=71&t=565933
http://www.sitepoint.com/forums/showthread.php?t=269085
http://www.quora.com/What-is-the-difference-between-functions-include-and-include_once-in-PHP
The include_once and require_once functions are slower than include and require, simply because they keep track of the files that have already been included, to avoid including them more than once.
But that shouldn't matter AT ALL, since there are probably many ways to optimize your application, way more efficient than this one.
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