PHP Include and Require
As PHP projects grow larger, keeping all the code in a single file becomes unmanageable. PHP provides the include and require statements to split code across multiple files. This allows for reusable components — such as a header, footer, navigation menu, or database connection — to be written once and used across many pages.
The include Statement
The include statement reads a file and inserts its contents at the point where the statement appears. If the file is not found, PHP shows a warning but continues executing the rest of the script.
<?php
// header.php
echo "<header><h1>My Website</h1></header>";
?>
<?php
// index.php
include "header.php";
echo "<p>Welcome to the homepage.</p>";
?>
When index.php runs, PHP inserts the content of header.php at that line, as if the code were written directly there.
The require Statement
The require statement works exactly like include, with one critical difference: if the file is not found, PHP generates a fatal error and stops execution immediately.
<?php
// database_connection.php
$host = "localhost";
$dbname = "myapp";
$user = "root";
$pass = "secret";
?>
<?php
// products.php
require "database_connection.php"; // Fatal error if file missing
echo "Database host: " . $host;
?>
Because the rest of the application cannot work without the database connection, require is the correct choice — it prevents the script from continuing with a missing critical file.
include vs require — When to Use Each
| Statement | File Not Found | Use For |
|---|---|---|
include | Warning, script continues | Optional files (ads, sidebar widgets) |
require | Fatal error, script stops | Critical files (config, DB connection) |
include_once | Warning, script continues | Optional files that should load only once |
require_once | Fatal error, script stops | Critical files that should load only once |
include_once and require_once
When a file is included multiple times — perhaps because it is included in a main file and also in a sub-file — PHP would load it repeatedly, causing function or class redefinition errors. The _once variants ensure the file is only loaded once per script execution.
<?php
// config.php - defines a constant
define("DB_HOST", "localhost");
?>
<?php
// helper.php - also includes config.php
require_once "config.php";
// ... helper functions
?>
<?php
// main.php
require_once "config.php"; // Loads config.php
require_once "helper.php"; // helper.php also requires config.php, but it's already loaded
echo DB_HOST; // Works without any errors
?>
Without require_once, the define("DB_HOST", ...) call would run twice, generating a PHP notice about the constant being already defined.
Practical File Structure
A typical PHP web project uses includes to create reusable page sections:
project/
├── config/
│ └── config.php
├── includes/
│ ├── header.php
│ ├── footer.php
│ └── nav.php
├── index.php
└── about.php
<!-- includes/header.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= $pageTitle ?? "My Site" ?></title>
</head>
<body>
<nav>
<a href="index.php">Home</a>
<a href="about.php">About</a>
</nav>
<!-- includes/footer.php -->
<footer>
<p>© <?= date("Y") ?> My Website. All rights reserved.</p>
</footer>
</body>
</html>
<?php
// index.php
$pageTitle = "Home Page";
require_once "config/config.php";
include "includes/header.php";
?>
<main>
<h1>Welcome to Our Website</h1>
<p>This is the homepage content.</p>
</main>
<?php include "includes/footer.php"; ?>
Using __DIR__ for Reliable Paths
When files are called from different directories, relative paths can break. Using __DIR__ (the magic constant that holds the directory of the current file) ensures paths always resolve correctly regardless of where the script is called from.
<?php
// Always resolves relative to THIS file's location
require_once __DIR__ . "/config/config.php";
include __DIR__ . "/includes/header.php";
?>
This is best practice for any project with files spread across multiple directories.
Variables Are Shared Across Includes
Variables defined before an include are available inside the included file. Variables defined inside the included file are available after the include in the parent script.
<?php
// greeting.php
echo "Hello, " . $username . "!";
?>
<?php
// main.php
$username = "Alice";
include "greeting.php"; // Outputs: Hello, Alice!
// Variables from the included file are also accessible here
?>
Key Points
includeinserts a file's content; if the file is missing, a warning appears but the script continues.requiredoes the same but stops execution with a fatal error if the file is missing.- Use
requirefor critical files (configuration, database connections) andincludefor optional components. - The
_oncevariants (include_once,require_once) prevent a file from being loaded more than once. - Use
__DIR__to build absolute paths that work regardless of the calling script's location. - Variables defined before an include are available inside the included file, and vice versa.
