Home » Web Development » Php » Include, Require & Require_once Statements

Include, Require & Require_once Statements

Include, Require & Require_once

Another way to make your code easier to use and maintain is to use include files. Actually, you’ve used them several times already and have seen how easy it is to bring external files into the current script and run them. All you have to do is use either the include or require constructs.

The only difference between the two is how they handle failure to bring in the external file. A failure of include results in a warning; a failure of require results in a fatal error.

There are a couple of reasons for using require_once instead of any of PHP’s other inclusion functions

  • If the package cannot be found, PHP throws a fatal exception and stops execution.
  • If the package has already been included by some other piece of code in the execution of this particular script, it won’t be included a second time, which greatly improves performance.

 

Above article is excerpt from “Beginning PHP5” by Dave Mercer et al.

Leave a comment