Difference between require, include, require_once and include_once ?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

require()

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop. when the file is required by your application, e.g. an important message template or a file containing configuration variables without which the app would break.

require_once()

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. when the file contains content that would produce an error on subsequent inclusion, e.g.function important() { /* important code */} is definitely needed in your application but since functions cannot be re-declared should not be included again.

include()

when the file is not required and application flow should continue when not found, e.g.
great for templates referencing variables from the current scope or something.

include_once()

optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead