Results 1 to 5 of 5

Thread: Is include_once as bad as require_once?

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    619
    Thanks
    274
    Thanked 14 Times in 14 Posts

    Default Is include_once as bad as require_once?

    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

  2. #2
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    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.

  3. The Following User Says Thank You to DyDr For This Useful Post:

    kuau (02-24-2018)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    619
    Thanks
    274
    Thanked 14 Times in 14 Posts

    Default

    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?

  5. #4
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    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.

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,496
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. images not showing with require_once
    By vineet in forum PHP
    Replies: 3
    Last Post: 12-01-2008, 06:54 AM
  2. include_once("../php_files/logout_links.php");
    By leonidassavvides in forum PHP
    Replies: 1
    Last Post: 08-16-2008, 08:15 PM
  3. do you ever use require_once() ?
    By Birmingham in forum PHP
    Replies: 6
    Last Post: 12-26-2006, 08:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •