Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The 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

Because require 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.
// Recommended syntax
require 'path/to/file.php';

// Valid, but generally discouraged
require('path/to/file.php');

Error Handling

The defining characteristic of require is its strict failure behavior. This is the primary technical distinction between require and include.
  • require: On failure, emits an E_COMPILE_ERROR and terminates the script.
  • include: On failure, emits an E_WARNING and allows script execution to continue.

Scope Inheritance

When a file is evaluated via require, 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 require statement occurs.
function loadConfiguration() {
    // Variables inside config.php are restricted to this function's local scope
    require 'config.php'; 
}

Return Values

By default, a successful require 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.
// data.php
<?php
return [
    'host' => 'localhost',
    'port' => 3306
];
// main.php
<?php
$config = require 'data.php';
// $config is now an array containing the returned data

Path Resolution

PHP resolves the file path provided to require using the following sequence:
  1. 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.
  2. If a bare filename or standard relative path is provided, PHP searches the directories specified in the include_path configuration directive.
  3. If not found in the include_path, PHP checks the directory of the calling script, followed by the current working directory.
  4. If all resolution attempts fail, the E_COMPILE_ERROR is thrown.
Using absolute paths, often constructed dynamically via the __DIR__ magic constant, bypasses the include_path search sequence entirely, resulting in faster file resolution.
require __DIR__ . '/src/bootstrap.php';
Master PHP with Deep Grasping Methodology!Learn More