> ## 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 Include Statement

The `include` statement is a PHP language construct that evaluates and incorporates the contents of a specified file into the execution flow of the calling script. When invoked, the PHP parser temporarily suspends execution of the current script, reads the target file, executes its contents within the current scope, and then resumes execution of the original script.

## Syntax

Because `include` is a special language construct and not a standard built-in function, parentheses are optional around the file path argument.

```php theme={"dark"}
// Without parentheses (preferred standard)
include 'filepath.php';

// With parentheses
include('filepath.php');

// Assigning a return value from an included file
$result = include 'filepath.php';
```

## Error Handling and Execution Flow

The defining characteristic of `include` is its error-handling behavior. If the PHP engine cannot locate or access the specified file, it emits an `E_WARNING` level error. The critical technical distinction is that **script execution continues** after this warning. This contrasts with the `require` construct, which emits a fatal `E_COMPILE_ERROR` and immediately halts execution.

## Scope and Context

Code within the included file inherits the variable scope of the exact line where the `include` statement occurs, but this inheritance applies strictly to variables.

* **Variables:** If `include` is called from within a function or method, the variables defined in the included file will be restricted to that local scope. If called in the global scope, the variables become available globally.
* **Functions, Classes, Interfaces, and Traits:** Regardless of where the `include` statement is executed (even within a local function scope), any functions, classes, interfaces, or traits defined within the included file are placed in the **global scope**, unless the included file contains a `namespace` declaration. If a namespace is declared, those structures are placed within that specific namespace. In either case, they become globally accessible, bypassing the local scope of the calling function.
* **Magic Constants:** Constants like `__FILE__` and `__DIR__` resolve to the included file's path and directory, not the calling file's context.

## Parsing Context

When an `include` statement is executed, the PHP parser drops out of PHP mode and enters HTML mode at the beginning of the target file. Therefore, any PHP code within the included file must be explicitly enclosed within valid PHP tags (`<?php ... ?>`). If the tags are omitted, the engine treats the file's contents as plain text/HTML and outputs it directly to the buffer.

## Path Resolution

PHP resolves the file path provided to the `include` statement using the following hierarchy:

1. **Absolute/Relative Paths:** If the path is absolute (e.g., `/var/www/file.php`) or explicitly relative (e.g., `./file.php` or `../file.php`), PHP looks only at that specific location.
2. **`include_path` Directive:** If only a filename or an implicit relative path is provided (e.g., `inc/file.php`), PHP searches the directories defined in the `include_path` configuration directive in `php.ini`.
3. **Calling Script Directory:** If the file is not found in the `include_path`, PHP checks the directory of the script that executed the `include` statement.
4. **Current Working Directory:** Finally, PHP checks the current working directory of the execution process before failing and emitting an `E_WARNING`.

## Remote File Inclusion

If the `allow_url_include` directive is enabled in the PHP configuration (`php.ini`), the `include` construct can fetch and execute files from remote sources using URL wrappers (such as `http://` or `ftp://`). However, the `allow_url_include` feature has been **deprecated since PHP 7.4**. When a remote file is included, PHP downloads the payload and executes any valid PHP code contained within it, provided the remote server does not parse the file before transmitting it. This distinguishes `include` from standard file-reading functions, as it actively evaluates the retrieved stream rather than just reading it as a string.

## Return Values

By default, the `include` construct returns `1` upon successful execution and `FALSE` on failure. However, the included file can use the `return` statement to terminate its own execution and pass a specific value back to the calling script. If a `return` statement is encountered inside the included file, the `include` expression evaluates to that returned value.

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