What is the difference between include, require, include_once, and require_once in terms of execution flow?
Last updated
All four pull in another file at runtime, but they differ in two axes: how they react to a missing/failed file (include vs require) and whether the file is loaded more than once (the _once variants).
Failure behavior:
include: on failure emits anE_WARNINGand the script continues executing.require: on failure emits anE_COMPILE_ERROR(fatal) and halts the script.
The
_oncevariants add deduplication:include_once/require_oncetrack already-included paths and skip a second load.This prevents redeclaration errors (functions, classes) and re-running top-level code.
Rule of thumb:
Use
require(orrequire_once) for files the app cannot run without (bootstrap, class definitions).Use
includeonly when the file is optional and a missing one shouldn't kill execution.
In practice, modern code uses an autoloader (Composer/PSR-4), so manual
require_onceis mostly limited to the bootstrap file.
- https://www.php.net/manual/en/function.include.php
- https://bugs.php.net/35176
- https://medium.com/@404_namenotfound/whats-the-difference-between-include-include-once-require-and-require-once-in-php-9b414c983967
- https://www.deviantdev.com/journal/php-difference-include-require-once
- https://anandbaraik.hashnode.dev/what-is-the-difference-between-include-and-require-in-php
- https://www.youtube.com/watch?v=d6InA-6F5iw
- https://www.php.net/manual/en/function.include-once.php
- https://www.php.net/manual/en/function.require-once.php