PHP Include
PHP include and require statements let you reuse code across files. Learn all four inclusion statements and best practices.
Introduction
PHP's include and require statements let you insert the contents of one PHP file into another before the server executes it. This is the foundation of code reuse in PHP: instead of copying a navigation bar, a database connection, or a set of helper functions into every page, you write it once in a separate file and pull it in wherever it's needed.
This chapter covers all four inclusion statements (include, require, include_once, require_once), how they differ, how included variables and functions behave, common gotchas, and the best practices that keep larger projects maintainable.
Why use file inclusion
Splitting code into reusable files gives you three concrete wins:
- Less repetition. Write a header, footer, or config block once and include it on as many pages as you need. Edit it in one place and every page updates.
- Easier maintenance. A bug fix or design change to shared markup happens in a single file rather than across dozens of pages.
- Better organization. Breaking a large script into smaller, purpose-named files (
header.php,db.php,functions.php) makes the codebase easier to read and navigate.
The four inclusion statements
PHP offers four statements. They all copy a file's contents into the current script, but they differ in how they handle a missing file and whether they allow re-inclusion.
| Statement | If the file is missing | Re-includes the same file? |
|---|---|---|
include | Emits a warning (E_WARNING); script keeps running | Yes |
require | Emits a fatal error (E_COMPILE_ERROR); script stops | Yes |
include_once | Warning; script keeps running | No — skipped if already included |
require_once | Fatal error; script stops | No — skipped if already included |
Rule of thumb: use require (or require_once) for files the page cannot work without — a database connection or a class definition. Use include for optional pieces, such as an ad slot or a sidebar that's nice to have but not critical.
Basic syntax
<?php
// Include a file relative to the current script
include __DIR__ . '/header.php';
// Stop the page if a critical file is missing
require __DIR__ . '/db.php';
// Guarantee a file is loaded at most once
require_once __DIR__ . '/functions.php';
?>__DIR__ is a magic constant that resolves to the directory of the current file. Building paths from it makes includes work no matter what directory the script is run from — see PHP Constants for more on magic constants.
Why _once matters
If you include a file twice that defines a function or class, PHP throws a "Cannot redeclare" fatal error on the second pass. include_once and require_once track which files have already been loaded and silently skip duplicates, so they're the safe default for any file that declares functions or classes.
<?php
require_once __DIR__ . '/functions.php'; // loads it
require_once __DIR__ . '/functions.php'; // skipped — no redeclare error
?>Variable scope in included files
An included file runs in the scope of the line where it was included. Variables defined before the include are available inside the included file, and variables the included file defines become available afterward.
<?php
// config.php
$siteName = "My Site";
$year = 2026;<?php
// index.php
include __DIR__ . '/config.php';
echo "Welcome to $siteName ($year)";
// Output: Welcome to My Site (2026)
?>Inside a function, however, an included file's variables are local to that function. This catches people out — a config file included inside a function won't leak its variables to the global scope.
Returning a value from an included file
An included file can return a value, which is handy for configuration arrays:
<?php
// settings.php
return [
'debug' => true,
'timezone' => 'UTC',
];<?php
// app.php
$config = include __DIR__ . '/settings.php';
echo $config['timezone']; // Output: UTC
?>Handling missing files gracefully
With include, a missing file only triggers a warning, so the rest of the page still renders. If a file is optional, check for it first:
<?php
$sidebar = __DIR__ . '/sidebar.php';
if (file_exists($sidebar)) {
include $sidebar;
} else {
echo "<!-- sidebar unavailable -->";
}
?>For files the page genuinely needs, prefer require so the script fails fast and loudly rather than producing broken output.
Best practices
- Use
require_oncefor code,includefor optional output. Function/class definitions should never be loaded twice; optional UI fragments can degrade gracefully. - Build paths from
__DIR__. Avoid relative paths likeinclude 'header.php', which depend on the current working directory and break unexpectedly. - Name files by purpose.
header.php,footer.php,db.php,auth.php— a glance at the filename should tell you what's inside. - Keep included files focused. One responsibility per file makes them easy to reuse and reason about.
- Never include a path built from user input (e.g.
include $_GET['page']). This opens a Local/Remote File Inclusion vulnerability. Whitelist allowed values instead.
Conclusion
The include and require family is how PHP projects stay DRY: shared markup, configuration, and logic live in single files and are pulled into pages on demand. Reach for require/require_once when a file is mandatory, include/include_once when it's optional, always anchor paths with __DIR__, and never include a user-controlled path. Next, see PHP Functions for organizing the reusable logic you'll typically place in included files.
Diagram
graph TD;
A[PHP Page]-->B[Included File];