> ## 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.

# PHP Require Statement

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.

```php theme={"dark"}
// 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.

```php theme={"dark"}
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.

```php theme={"dark"}
// data.php
<?php
return [
    'host' => 'localhost',
    'port' => 3306
];
```

```php theme={"dark"}
// 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.

```php theme={"dark"}
require __DIR__ . '/src/bootstrap.php';
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
