20. PHP Include and Require
What you will learn
- How to reuse code across files
- The four statements:
include,require, and their_onceversions - How to build file paths safely with
__DIR__ - How to build a simple page template
- The security rules for including files
Why split code into files?
A website repeats the same header, menu, and footer on every page. Instead of copying them everywhere, put each in its own file and include it. Change the file once, and every page updates. Functions and settings can be shared the same way.
The four statements
| Statement | If the file is missing | Loads the same file twice? |
|---|---|---|
include |
Warning, and the script continues | Yes |
require |
Fatal error, and the script stops | Yes |
include_once |
Warning, and the script continues | No, only the first time |
require_once |
Fatal error, and the script stops | No, only the first time |
Use require for files the page cannot work without, and include for optional pieces. Use the _once versions for files that define functions or classes, because defining the same function twice is a fatal error.
A first example
Folder structure
practice/
index.php
includes/
header.php
footer.php
PHP code: includes/header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($page_title ?? "My site", ENT_QUOTES, "UTF-8") ?></title>
</head>
<body>
<header>
<nav><a href="index.php">Home</a> | <a href="about.php">About</a></nav>
</header>
<main>
PHP code: includes/footer.php
</main>
<footer>
<p>© <?= date("Y") ?> My site</p>
</footer>
</body>
</html>
PHP code: index.php
<?php
$page_title = "Home";
require __DIR__ . "/includes/header.php";
?>
<h1>Welcome</h1>
<p>This page is built from three files.</p>
<?php require __DIR__ . "/includes/footer.php"; ?>
What the browser receives (the year depends on today’s date)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<header>
<nav><a href="index.php">Home</a> | <a href="about.php">About</a></nav>
</header>
<main>
<h1>Welcome</h1>
<p>This page is built from three files.</p>
</main>
<footer>
<p>© 2026 My site</p>
</footer>
</body>
</html>
An included file behaves as if its text had been pasted at that spot. It also shares variables with the page that includes it, which is how $page_title reaches the header. Every other page just changes the title and includes the same two files.
Why __DIR__?
__DIR__ is a built-in constant holding the folder of the file that is running. Joining it with a path makes the address absolute, so the include works from any folder.
PHP code
<?php
// Fragile: depends on where PHP was started
require "includes/header.php";
// Reliable: always relative to this file
require __DIR__ . "/includes/header.php";
Sharing functions
Put helper functions in one file, and load it with require_once, so it is never loaded twice by accident.
PHP code: includes/functions.php
<?php
function e(string $text): string {
return htmlspecialchars($text, ENT_QUOTES, "UTF-8");
}
function price(float $amount): string {
return "$" . number_format($amount, 2);
}
PHP code: shop.php
<?php
require_once __DIR__ . "/includes/functions.php";
echo e("Tom & Jerry") . " costs " . price(4.5);
What the browser shows
Tom & Jerry costs $4.50
Files that return a value
An included file can use return, and the including line receives that value. This is a neat way to load settings.
PHP code: includes/settings.php
<?php
return [
"site_name" => "BeingXpert",
"items_per_page" => 10,
];
PHP code: settings-demo.php
<?php
$settings = require __DIR__ . "/includes/settings.php";
echo $settings["site_name"] . ": " . $settings["items_per_page"] . " per page";
What the browser shows
BeingXpert: 10 per page
What happens if a file is missing?
PHP code
<?php
include __DIR__ . "/does-not-exist.php";
echo "Still running.";
What you should see (the warning text and path will differ)
A warning that the file could not be opened, followed by: Still running.
With require instead, the script stops at that line with a fatal error and “Still running.” never appears. That is why require is the right choice for essential files.
Security rules
Never build an include path from visitor input. Code such as include $_GET["page"] . ".php"; lets an attacker choose which file runs, a serious vulnerability known as file inclusion. Use an allowed list instead.
PHP code
<?php
$allowed = ["home", "about", "contact"];
$page = $_GET["page"] ?? "home";
if (!in_array($page, $allowed, true)) {
$page = "home";
}
require __DIR__ . "/pages/" . $page . ".php";
- Only include files you wrote and placed on the server.
- Keep files that hold secrets, such as database passwords, outside the folder the web server publishes, or block them in the server settings.
- Do not let uploaded files end up in a folder where they can be included or run.
- Give included files a
.phpending. A.incor.txtfile may be shown as plain text to anyone who requests it, revealing your code.
How WordPress uses this
WordPress themes are built from included parts. get_header(), get_footer(), and get_template_part(), which this very site uses, are wrappers around the same idea.
Common mistakes
- Using a relative path without
__DIR__, so it breaks when the page is opened from another folder. - Using
includefor an essential file, so the page carries on half-built. - Declaring a function in a file included twice. Use
require_once. - Blank lines after a closing
?>in an included file, which are sent as output and can breakheader(). - Including a path built from visitor input.
- Storing secrets inside the public folder.
Exercise
Create includes/greeting.php that defines a function greeting($name) returning Hello, and the name. Then write page.php that loads it with require_once and prints the greeting for Sam.
Show solution
<?php
// includes/greeting.php
function greeting(string $name): string {
return "Hello, " . htmlspecialchars($name, ENT_QUOTES, "UTF-8");
}
<?php
// page.php
require_once __DIR__ . "/includes/greeting.php";
echo greeting("Sam");
The output is Hello, Sam.
Knowledge check
1. What is the difference between include and require when a file is missing?
include gives a warning and carries on. require causes a fatal error and stops.
2. Why use require_once for a functions file?
It prevents the file loading twice, which would define the same functions again and cause a fatal error.
3. Why is include $_GET["page"] . ".php" dangerous?
A visitor chooses which file runs. Use an allowed list instead.
Summary
- Include shared pieces such as headers, footers, and functions instead of copying them.
- Use
requirefor essential files,_oncefor function files, and__DIR__for reliable paths. - Never include a path chosen by a visitor, and keep secrets out of the public folder.