PHP Include and Require — Complete Guide to File Inclusion
In this tutorial, you will learn about PHP Include and Require. We cover key concepts, practical examples, and best practices to help you master this topic.
PHP include and require statements insert the content of one PHP file into another, enabling code reuse, modular organization, and Separation Of Concerns.
What You'll Learn
By the end of this tutorial, you'll use include, require, include_once, and require_once, understand their differences, manage file paths, and organize code into reusable components.
Why Include/Require Matters
Without file inclusion, every PHP page would repeat the same code for headers, footers, database connections, and utility functions. Include statements make code DRY and maintainable.
Real-World Use
A website uses a header.php for navigation, footer.php for copyright, and config.php for database settings. Each page includes these components rather than duplicating the code.
Include/Require Learning Path
flowchart LR
A[Loops] --> B[Include/Require]
B --> C[Superglobals]
C --> D[Forms]
D --> E[Sessions]
A --> F{You Are Here}
style F fill:#f90,color:#fff
Basic Inclusion
<?php
// config.php
$dbHost = "localhost";
$dbName = "myapp";
define("SITE_NAME", "My Website");
// index.php
include "config.php";
echo SITE_NAME; // My Website
echo $dbHost; // localhost
Include vs Require
<?php
// include — warning on failure, script continues
include "missing.php"; // Warning: file not found
echo "This still runs";
// require — fatal error on failure, script stops
require "missing.php"; // Fatal error
echo "This never runs";
Include Once and Require Once
<?php
// prevent multiple inclusions of the same file
require_once "config.php";
require_once "config.php"; // Skipped — already included
// This prevents:
// - Redefined functions/classes
// - Duplicate variable assignments
// - Redefined constants
File Path Resolution
<?php
// Relative to current file
include "header.php"; // Same directory
include "includes/header.php"; // Subdirectory
include "../header.php"; // Parent directory
// Absolute path
include "/var/www/includes/header.php";
// Using __DIR__ (recommended)
include __DIR__ . "/includes/header.php";
Return Values from Included Files
<?php
// data.php
return ["name" => "Alice", "age" => 25];
// index.php
$user = include "data.php";
echo $user["name"]; // Alice
Template Example
<?php
// header.php
?>
<!DOCTYPE html>
<html><head><title><?= $pageTitle ?? "Default" ?></title></head><body>
<nav><a href="/">Home</a> | <a href="/about">About</a></nav>
<?php
// footer.php
?>
<footer>© <?= date("Y") ?> My Website</footer></body></html>
<?php
// page.php
$pageTitle = "About Us";
include "header.php";
echo "<h1>About Us</h1><p>Content here.</p>";
include "footer.php";
Common Mistakes
1. Using include Instead of require for Critical Files
Database connection files should use require. If the connection file is missing, the app should crash, not proceed.
2. Not Using DIR for Relative Paths
include "config.php" depends on the working directory, which changes when files in subdirectories include other files.
3. Including User-Controlled Paths
Allowing users to specify which file to include enables remote file inclusion (RFI) attacks.
4. Including HTML Without Namespace
Variables declared in included files become global. Wrap in functions or classes to avoid pollution.
5. Forgetting require_once for Functions/Classes
Including a function definition twice causes a fatal error. Always use require_once for functions and classes.
Practice Questions
1. What is the difference between include and require?
include produces a warning on failure and continues. require produces a fatal error and stops execution.
2. When should you use require_once instead of require?
Use require_once when the file defines functions, classes, or constants that would cause errors if defined twice.
3. Why use DIR in include paths?
DIR ensures the path is relative to the current file, not the working directory, preventing bugs in nested includes.
4. Can included files return values?
Yes. An included file can return a value which becomes the result of the include expression.
5. Challenge: Create a simple template system using include with variables.
<?php
// layout.php
?>
<!DOCTYPE html>
<html><head><title><?= $title ?></title></head>
<body><?= $content ?></body></html>
<?php
// page.php
$title = "Welcome";
ob_start();
echo "<h1>Hello World</h1>";
$content = ob_get_clean();
include "layout.php";
FAQ
Mini Project: Modular Website
Create a modular website structure using include and require.
<?php
// config.php
define("SITE_NAME", "ModularSite");
define("DB_HOST", "localhost");
// functions.php
function renderHeader(string $title): void {
include __DIR__ . "/templates/header.php";
}
function renderFooter(): void {
include __DIR__ . "/templates/footer.php";
}
// index.php
require_once "config.php";
require_once "functions.php";
renderHeader("Home");
echo "<h1>Welcome to " . SITE_NAME . "</h1>";
renderFooter();
What's Next
PHP Superglobals PHP GET POST PHP Sessions
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro