1
require("file.php");
require_once("file.php");

Both includes the required files and gives fatal error if file is not found or any other problem is there. Both stops the execution if error occurs.

what is the difference between these two functions ?

TheRocky
  • 53
  • 6
  • Have you read the [documentation of `require_once`](http://php.net/manual/en/function.require-once.php)? – axiac May 02 '18 at 05:39
  • *"what is the difference between these two functions ?"* -- they are not functions but language constructs (statements). – axiac May 02 '18 at 05:39
  • Possible duplicate of [Difference between require, include, require\_once and include\_once?](https://stackoverflow.com/questions/2418473/difference-between-require-include-require-once-and-include-once) – BharathRao May 02 '18 at 05:49

2 Answers2

6

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

Yash M. Hanj
  • 495
  • 1
  • 3
  • 15
0

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. REF: Difference between "include" and "require" in php

Coder
  • 11
  • 1
  • 6