Difference between include_once and require_once
Can you please explain me difference between include_once() and require_once() with example?
QuickBooks Hosting Provider
QuickBooks Hosting Services
require[_once]() generates a fatal error if unable to load the specified file, while include[_once]() just generates a non-fatal error and the script keeps running.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
- include() includes a file in memory no matter if it has already been loaded
- include_once() includes a file in memory only if it has not been loaded already
- require() includes a file in memory no matter if it has already been loaded. if the file cannot be loaded, the script is aborted
- require_once() does the same as require(), but only if the file hasn't been included yet. if the file's already in memory, it has no effect.
Include_once:includes and assesses the specified index and if the code from a document has recently been incorporated, it won't be incorporated once more
Require_once: is indistinguishable to incorporate() aside from upon washout it will handle a deadly E_error level mistake. PHP will check if the record has as of recently been incorporated, and assuming this is the case, not incorporate (require) it once more.
Require_once throws a fatal error if the file is not found, whereas include_once only show a warning and continue to load the rest of the page in php.
If you don't want PHP to attempt to load the rest of your page without the database information , then use require_once.
Include_once and require_once do the same thing, but only if the file was not already loaded.
Require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
So, require_once() is recommended to use when you want to include a file where you have a lot of functions.
Hope this helps.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)