TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
require statement is a PHP language construct that includes and evaluates a specified file during the execution of a script. It acts as a strict inclusion mechanism; if the target file cannot be located, read, or evaluated, PHP emits a fatal E_COMPILE_ERROR and immediately halts the execution of the calling script.
Syntax
Becauserequire is a language construct and not a standard function, parentheses around the file path are optional. The PHP-FIG standards (PSR-12) recommend omitting the parentheses.
Error Handling
The defining characteristic ofrequire is its strict failure behavior. This is the primary technical distinction between require and include.
require: On failure, emits anE_COMPILE_ERRORand terminates the script.include: On failure, emits anE_WARNINGand allows script execution to continue.
Scope Inheritance
When a file is evaluated viarequire, the parser temporarily drops out of PHP mode and into HTML mode at the beginning of the target file, resuming PHP mode at the end.
The included code inherits the variable scope of the exact line where the require statement is invoked:
- If called within the global scope, the variables in the required file exist in the global scope.
- If called within a function or method, the variables in the required file exist within that local scope.
- Functions and classes defined within the required file are always placed in the global scope, regardless of where the
requirestatement occurs.
Return Values
By default, a successfulrequire statement returns 1. However, the required file can execute a return statement within its global scope to pass a specific value back to the calling script. When this occurs, the require statement evaluates to that returned value.
Path Resolution
PHP resolves the file path provided torequire using the following sequence:
- If an absolute path (starting with
/,\, or a Windows drive letter) or a relative path explicitly starting with./or../is provided, PHP looks exactly at that location. - If a bare filename or standard relative path is provided, PHP searches the directories specified in the
include_pathconfiguration directive. - If not found in the
include_path, PHP checks the directory of the calling script, followed by the current working directory. - If all resolution attempts fail, the
E_COMPILE_ERRORis thrown.
__DIR__ magic constant, bypasses the include_path search sequence entirely, resulting in faster file resolution.
Master PHP with Deep Grasping Methodology!Learn More





