31

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?

FarwallGhost
  • 688
  • 3
  • 7
  • 14

4 Answers4

53

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.

Norguard
  • 26,167
  • 5
  • 41
  • 49
  • 4
    This doesn't answer the question - "What's the difference between `require_once()` and `include_once()`?". – Amal Murali Feb 16 '14 at 16:58
  • 1
    So, What is the best way to Write Include or Require? @Norguard – FarwallGhost Feb 16 '14 at 17:04
  • 4
    @AmalMurali That is exactly the answer, in a human understandable format: `include` will continue execution with a warning, `require` will terminate with a fatal error, in the case the resource is missing. Perhaps you could read those first two statements again. Also, he's not asking for the delta between `include` and `include_once`, so for the sake of this discussion, the *whole* difference between `include` and `require` applies to all instances thereof. Convention over configuration. – Norguard Feb 16 '14 at 17:04
  • @FarwallGhost: Please see [this answer](http://stackoverflow.com/a/2418580/). – Amal Murali Feb 16 '14 at 17:07
  • It doesnt answer the question.. – Sam Nov 02 '15 at 07:23
22

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.

simmer
  • 2,639
  • 1
  • 18
  • 22
Banago
  • 1,350
  • 13
  • 22
6

The difference is that require_once() will produce a Fatal Error on failure, while include_once() only produces a warning.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

page stop execute require_once can't be load required file and on the other hand include_once will continue instead of error in loading including file.

sabbir
  • 2,020
  • 3
  • 26
  • 48