Introduction
When building PHP applications, you often need to reuse the same code across multiple pages. For example, most websites have a common header, footer, or database configuration file used on every page.
Instead of repeating the same code again and again, PHP provides include and require statements to reuse files efficiently.
These statements are essential for writing clean, maintainable, and scalable PHP applications.
In this tutorial, you’ll learn how include and require work, the difference between them, when to use each, and how to apply them in real-world projects with simple examples.
What You’ll Learn
- What include and require are
- Difference between include vs require
- include_once and require_once
- Real-world examples
- Common mistakes to avoid
What is PHP Include?
The include statement is used to insert the contents of one PHP file into another file.
Example:
include 'header.php';
👉 This will include the header.php file into your current script.
Important Behavior:
- If the file is not found, PHP shows a warning
- The script continues running
👉 So, include is useful when the file is not critical
What is PHP Require?
The require statement works similar to include, but with one key difference.
Example:
require 'header.php';
Important Behavior:
- If the file is missing, PHP throws a fatal error
- The script stops execution immediately
👉 So, require is used for important files
Why Use Include & Require?
Without include/require, you might repeat the same code on multiple pages.
With them, you write the code once and reuse it everywhere.
Example:
header.phpfooter.phpconfig.php
The Problem Include & Require Solve
Imagine you’re building a website project with 10–15 main pages. Each page has some unique content, but also shares common sections (like header, footer, or sidebar). Later, when you want to update that common content, you would have to edit it separately on all 10–15 pages.
Instead, if you place the shared content in a single file and include it on every page, you only need to update that one file — and the changes will automatically appear everywhere.
Important Point
If the file is not found, PHP will show a warning, but the script will continue running.
include_once & require_once
These are used to prevent loading the same file multiple times.
Example
include_once "functions.php";
require_once "config.php";
Difference Between include and require
| Feature | include | require |
|---|---|---|
| File not found | Warning | Fatal error |
| Script execution | Continues | Stops |
| Use case | Optional files | Mandatory files |
Real-Life Example: Website Structure
Practical Folder Structure Example
/includes
header.php
footer.php
db.php
index.php
about.php
header.php
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<header>Header Section</header>
footer.php
<footer>Footer Section</footer>
</body>
</html>
index.php
<?php
include "includes/header.php";
echo "<h1>Home Page</h1>";
echo "<p>Welcome to my website</p>";
include "includes/footer.php";
?>
When to Use What?
✔ Use include for:
- Footers
- Sidebars
- Optional sections
✔ Use require for:
- Database connections
- Configuration files
- Core functions
Common Mistakes to Avoid
- Wrong file paths
- Including the same file multiple times
- Using
includeinstead ofrequirefor critical files
FAQs
Both include and require are used to insert one PHP file into another PHP file. The main difference is that include gives a warning if the file is missing, but the script continues running. Require gives a fatal error if the file is missing, and the script stops immediately.
Use include when the file is optional and the script can continue even if that file is missing. Common examples are sidebar files, advertisement sections, and optional content blocks.
Use require when the file is essential for your application. Common examples are database connection files, configuration files, and authentication files. If these files are missing, the script should stop.
include_once and require_once work like include and require, but they make sure the file is included only one time. This helps prevent function redeclaration errors and class duplication issues. They are very useful in large PHP projects.
It depends on your use case. Use require for critical files and include for optional files. In most real-world projects, require_once is commonly used for important files like database connections and configuration files.
Yes, both can be used with PHP files, HTML files, and other reusable template files. They are commonly used for headers, footers, navigation menus, and reusable layout sections to keep code clean and organized.
Mini Task for Students
- Create
header.phpandfooter.php - Include them in
index.php - Create a
db.phpfile - Use
require_onceto connect database - Try breaking the path and see how include vs require behaves
Summary
In this tutorial, you learned how PHP include and require help reuse code and build clean applications.
Include is useful for optional files, while require is used for critical files where execution must stop if the file is missing.
You also learned how include_once and require_once prevent duplicate file loading and why they are important in real-world projects.
Using these concepts will help you write more organized, maintainable, and professional PHP code.
In the next tutorial, we’ll learn about Introduction to PHP File Handling.
