W3docs

include

The "include" keyword is used in PHP to include a file in the current script. In this article, we will explore the syntax and usage of the "include" keyword in

The PHP include Statement

include pulls the contents of another PHP file into the current script and runs it at the point where the statement appears. Conceptually, PHP copies the included file in place, evaluates it in the same scope, and continues. This is how you avoid repeating shared markup and logic — headers, footers, navigation, configuration, and reusable functions — across many pages.

This page covers the syntax, how scope works, what include returns, how it differs from require, and the common mistakes to avoid.

Syntax

include 'filename.php';

The path can be relative or absolute. include is a language construct, not a function, so the parentheses are optional — both include 'file.php'; and include('file.php'); work.

When PHP runs an include, it searches for the file in this order: the path given (if relative, resolved against the current working directory and the include_path setting), then the directory of the running script. To make includes robust regardless of where the entry script is called from, anchor them to the current file:

include __DIR__ . '/partials/header.php';

__DIR__ is the directory of the file containing the statement, so the include resolves correctly even when the script is run from another directory.

A working example

Because include operates on separate files, the cleanest way to see it in action is to create those files at runtime. The snippet below writes a small partial, includes it, and prints the result — it is fully runnable as a single script:

<?php
// Create a reusable partial on disk.
file_put_contents(__DIR__ . '/greeting.php', '<?php echo "Hello, " . $name . "!"; ?>');

// Variables defined here are visible inside the included file (shared scope).
$name = "Ada";

echo "Page top\n";
include __DIR__ . '/greeting.php';   // runs greeting.php here
echo "\nPage bottom\n";

// Output:
// Page top
// Hello, Ada!
// Page bottom

The included file can read $name because include shares the surrounding scope. This is the most important thing to understand about include: it is not an isolated module system. Whatever variables exist before the include are available to the included file, and any variables the included file defines leak back into the caller.

Returning a value from an included file

An included file can return a value, which becomes the result of the include expression. This is the idiomatic way to load configuration:

<?php
// Write a config file that returns an array.
file_put_contents(__DIR__ . '/config.php', '<?php return ["env" => "prod", "debug" => false];');

$config = include __DIR__ . '/config.php';

echo $config["env"];        // prod
echo "\n";
var_export($config["debug"]); // false

// Output:
// prod
// false

If the included file does not return, the include expression evaluates to 1 on success.

include vs require (and the _once variants)

PHP has four related statements. They all pull in a file; they differ in what happens on failure and whether duplicate includes are skipped.

StatementIf the file is missingLoads the file again if already loaded
includeEmits an E_WARNING and the script continuesYes
requireEmits an E_ERROR and the script haltsYes
include_onceWarning, continuesNo — skipped if already included
require_onceFatal error, haltsNo — skipped if already included

Rules of thumb:

  • Use require for files the script cannot run without (a config file, a class definition, a database connection). Failing fast with a fatal error is safer than continuing in a broken state.
  • Use include for optional pieces, such as a sidebar widget or an ad block, where a missing file should not bring down the whole page.
  • Use the _once variants when re-including a file would cause errors — most commonly when it declares functions or classes, since redeclaring them is a fatal error.

Common pitfalls

  • Trusting user input in a path. Never pass unfiltered request data to include (for example include $_GET['page'] . '.php';). It opens the door to local file inclusion attacks. Validate against an allow-list of known files instead.
  • Relative paths that break. A bare include 'header.php'; depends on the current working directory and can fail when the script is invoked from elsewhere. Prefer __DIR__-anchored paths.
  • Redeclaring functions or classes. If a file that defines functions is pulled in twice via plain include, PHP throws a "Cannot redeclare" fatal error. Use include_once for such files.
  • Assuming isolation. Because scope is shared, an included file can overwrite your variables. Keep includes focused and predictable.

Why use include

  • Reusability — define shared markup or logic once and pull it into every page that needs it.
  • Maintainability — change the navigation in one partial and every page updates.
  • Structure — split a large application into small, focused files instead of one monolith.

Practice

Practice
In PHP, what is the function of the 'include' statement?
In PHP, what is the function of the 'include' statement?
Was this page helpful?