What is difference between "include_once" and "require_once" in PHP?
include_once "connect_to_mysql.php"; require_once "connect_to_mysql.php";
Is there any difference between these?
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 basic difference between require and require_once is require_once will check whether the file is already included or not if it is already included then it won't include the file whereas the require function will include the file irrespective of whether file is already included or not.
The include_once keyword is used to embed PHP code from another file. If the file is not found, a warning is shown and the program continues to run. If the file was already included previously, this statement will not include it again.
The include_once statement includes and evaluates the specified file during the execution of the script. 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 .
Include will let the script keep running (with a warning) if the file is missing.
Require will crash if it's missing.
If you're using include for things which are 100% optional, then include will still work and require will explode.
If you include something that you think is optional, but some other piece of your script uses it, way down the line, then your script will explode there, and you'll probably have no idea why.
This is not a good way to write programs.
Otherwise, there isn't a difference between the two.
In typical usage, it shouldn't matter whether you choose to use include
or require
, 95% of the time.
As such, you should stick to require
(or require_once
), to tell you when you're missing a file you need.
The bugs that come from including files inside of included files, inside of included files, when one include up top is missing, are really hard to track down.
Some people prefer include
, because they want to use that "feature".
Douglas Crockford is a JavaScript/Java/C++ guy, rather than PHP, but he suggests that features that look like bugs, or side effects which are indistinguishable from bugs, should be avoided for your sanity.
Mind you, if your entire project is completely class-based (or functional), and entirely modular, then you shouldn't have much use for include, aside from, say resetting values on a configuration-object at application initialization (including admin-override options or dev-debugging options) on an object that was already require
d.
As you might guess, there are other ways of doing this.
And these are just suggestions, but suggestions that come from people who are smarter than I am, with decades of experience in dozens of languages.
include_once
will throw a warning, but will not stop PHP from executing the rest of the script.
require_once
will throw an error and will stop PHP from executing the rest of the script.
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