Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between "include_once" and "require_once" in PHP? [duplicate]

Tags:

php

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?

like image 410
FarwallGhost Avatar asked Feb 16 '14 16:02

FarwallGhost


People also ask

What is the difference between include_once () and require_once () in PHP?

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.

What is the main difference between require () and require_once () in PHP?

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.

What does Include_once mean in PHP?

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.

What is the difference between include and include_once statements?

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 .


2 Answers

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.

edit

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 required.

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.

like image 98
Norguard Avatar answered Oct 20 '22 01:10

Norguard


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.

like image 21
Banago Avatar answered Oct 20 '22 00:10

Banago