View Full Version : Is include_once as bad as require_once?

02-24-2018, 09:22 PM
Dear Daniel: In your php Tips, #7 says "require_once() is expensive." Does that mean "include_once()" is expensive in the same way? Is plain "include()" better? Thanks. aa :)

02-24-2018, 10:24 PM
Both of the _once versions check a table of all the included/required files to see if the requested file has already been loaded. If the file has not already been loaded, it is. It's the extra time needed to check this table that makes the _once versions more expensive to use in well written code.

The non _once() versions just unconditionally load the requested file.

If your code is properly organized, so that you are only including/requiring any particular file once, using the _once versions will end up using more processing time. If your code is not well organized, and you are using _once versions to avoid loading the same file multiple times, it will end up saving the i/o time needed to load a file that has already been loaded.

As to using require v.s. include. The only difference is that require will produce a fatal run-time error if the file cannot be found/loaded. You should use require for things that your code requires for it to work. This would include (pun intended) most things, such as configuration values, php code, html templates... You would use include for optional things that you don't want your code to end execution on if they don't exist.

02-24-2018, 11:40 PM
Excellent reply... thank you! My code is very organized (lean & mean) so I never needed the "_once." I mistakenly thought that it was 'best practices' to use it so I am happy to be able to remove it. I have never used "require." Wouldn't a regular error message be better than a fatal error?

02-25-2018, 12:08 AM
If your code requires something for the site to work properly, halting execution will prevent follow-on errors and unintended operation that could alter or corrupt other things. A missing file is an exception and if it occurs in fully tested code means that somehow a file got deleted, moved, or renamed or if you are dynamically requiring expected files, would mean a coding mistake. These would be rare occurrences and you would want the php errors, being logged on a live site, to stop at the error for the missing file, rather than to fill up the error log with 10's or 100's of follow-on errors as the result of the rest of the code trying to use things that are in the missing file.

02-25-2018, 12:31 AM
Probably just repeating what's already been said, personally I wouldn't worry about it unless there's a chance it would be included/required more than once. Well conceived flow would never have occasion to worry about that. But if the possible double, triple, etc., inclusions aren't too deep, I don't see as it does any harm. If they go out to more than 10, it's time to consider rewriting/reorganizing things. I think code providers use these because you may also have an earlier version of their or someone's similar code.