What is the difference between PHP require and include?

  • include includes a file and throws a warning if the file was not found.

  • require includes a file and throws a fatal error if the file was not found.

  • include_once and require_once do the same thing, but only if the file was not already loaded.

However, the need for one of the _once functions is usually a sign of bad design. You should build your scripts in a way that clearly defines what gets included where.

Choose one place for settings.php to get included - probably index.php. There should be no need to additionally include it in database.php.


The difference between include() and require() arises when the file being included cannot be found: include() will raise a warning (E_WARNING) and the script will continue, whereas require() will raise a fatal error (E_COMPILE_ERROR) and halt the script. If the file being included is critical to the rest of the script running correctly then you need to use require().

For more details : Difference between Include and Require in PHP


You don't load settings.php two times, as per PHP documentation on 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.

Tags:

Php