Require and Include
In PHP, the include and require statements are used to load the contents of one file into another. When a file is included, all of its code is executed as if it were written directly in the current file. This is a fundamental technique for organizing larger applications and avoiding duplicated code.
The key difference between include and require is how PHP behaves when the file cannot be found:
includetriggers a warning, but the script continues executing.requiretriggers a fatal error, and the script stops immediately.
This distinction is intentional and should be used to express importance:
- Use
requirefor files that are essential for the application to work, such as configuration files, core helpers, or database setup. - Use
includefor files that are optional or presentational, such as headers, footers, admin notices, or UI components.
Included files share the same variable scope as the file that includes them. This means variables defined in an included file are available afterward, and included files can also rely on variables defined before the include statement.
In real WordPress plugins, include and require are used to split large systems into smaller, readable files while controlling whether execution should continue or stop when something goes wrong.