🐘 PHP Intermediate

What is the difference between include_once and require_once?

Answer

Both include_once and require_once include a file only once — if the file has already been included, PHP skips including it again (preventing function/class redeclaration errors). The only difference is what happens when the file cannot be found: include_once generates a warning (E_WARNING) and continues script execution. require_once generates a fatal error (E_COMPILE_ERROR) and halts execution. Use require_once for files that the application cannot function without (autoloaders, configuration files, base classes). In modern PHP with Composer autoloading, you rarely need to call these manually — the autoloader handles file inclusion automatically.