-
Hajipur, Bihar, 844101
When building a website, you often repeat the same parts of code — such as a header, footer, or navigation menu — across multiple pages. Writing the same HTML or PHP again and again not only takes time but also makes maintenance harder.
PHP’s include and require statements help you solve this problem. They let you reuse code by including files inside other PHP files.
With includes, you can create modular and manageable websites, where changes made in one file automatically appear everywhere that file is included.
The include statement is used to insert the content of one PHP file into another before the server executes it.
It’s like copying the code from one file and pasting it into another at runtime — but in a much cleaner and dynamic way.
For example, imagine Vrinda is building a website with five pages: home.php
, about.php
, services.php
, contact.php
, and gallery.php
. Each of these pages has the same header and footer. Instead of repeating the same code on all five pages, she can create two separate files:
header.php
footer.php
Then, she can include them in every page using PHP’s include function.
<?php include 'filename.php'; ?>
When the PHP engine encounters this statement, it stops for a moment, reads the file you mentioned, and executes it as if its contents were part of the current page.
Let’s say Ananya creates a simple header and footer.
<!DOCTYPE html>
<html>
<head>
<title>My PHP Website</title>
</head>
<body>
<header>
<h1>Welcome to Ananya’s Website</h1>
<hr>
</header>
<hr>
<footer>
<p>© 2025 Ananya’s Website. All rights reserved.</p>
</footer>
</body>
</html>
Now she can include them in any main page.
<?php include 'header.php'; ?>
<h2>Home Page</h2>
<p>This is the homepage content managed by Ananya.</p>
<?php include 'footer.php'; ?>
When the browser loads home.php
, PHP includes both header.php
and footer.php
. The output looks like one complete HTML document.
Reusability: You write the code once and use it on multiple pages.
Easy Maintenance: Updating one file automatically updates all pages using it.
Cleaner Code: Keeps your main files short and organized.
Separation of Logic and Layout: You can separate design from PHP logic easily.
PHP provides both include
and require
for including files, but there’s a small difference between them.
Statement | When File Is Missing | Execution Continues? |
---|---|---|
include | Gives a warning | Yes |
require | Gives a fatal error | No |
Example:
<?php
include 'menu.php'; // if file not found → Warning but script continues
require 'config.php'; // if file not found → Fatal error, script stops
?>
So, if Riya is including a non-essential part of her website, like a sidebar, she can use include
.
But for important files like database configuration, it’s better to use require
.
If you accidentally include the same file multiple times, PHP will load it again and again — possibly causing function redefinitions or variable overwrites.
To prevent this, PHP provides include_once
and require_once
.
include_once
includes a file only if it hasn’t been included before.
require_once
does the same but stops execution if the file is missing.
Example:
<?php
include_once 'header.php';
include_once 'header.php'; // ignored the second time
?>
This ensures that the file is included only once, even if your code tries to include it twice.
Imagine Vrinda is developing a blog. She can create small reusable files for:
Header (header.php
)
Sidebar (sidebar.php
)
Footer (footer.php
)
Database connection (db_connect.php
)
Then her main blog post page (post.php
) can include all of them:
<?php
include 'header.php';
include 'sidebar.php';
include 'db_connect.php';
?>
<h2>Welcome to Vrinda’s Blog</h2>
<p>This post was written on <?php echo date("F j, Y"); ?>.</p>
<?php include 'footer.php'; ?>
This keeps the layout consistent and the code easy to manage.
You can also include files dynamically using variables.
<?php
$page = 'about';
include $page . '.php';
?>
Here, if $page
is set to 'about'
, PHP will include about.php
.
This is often used in content management systems where pages are loaded based on user input.
However, never include files directly from user input without validation, as it may open security vulnerabilities.
Incorrect file paths:
Always make sure the file path is correct relative to the current script.
Mixing HTML and PHP incorrectly:
Ensure that included files fit properly into your HTML structure (no extra <html>
or <body>
tags).
Forgetting to close tags:
If your header.php
starts <body>
and footer.php
ends it, every main page should include both.
Including sensitive files in public directories:
Keep important include files (like config files) outside the web root for security.
Let’s build a complete example where Riya creates a product page with header.php
, footer.php
, and product.php
.
<!DOCTYPE html>
<html>
<head>
<title>Riya’s Shop</title>
</head>
<body>
<header>
<h1>Riya’s Online Store</h1>
<hr>
</header>
<hr>
<footer>
<p>Copyright © 2025 Riya’s Shop</p>
</footer>
</body>
</html>
<?php
require 'header.php'; // stops if file missing
?>
<h2>Product: Handmade Earrings</h2>
<p>These earrings are crafted by Riya and available for Rs. 599.</p>
<?php
include 'footer.php'; // only shows warning if missing
?>
This keeps the project structured, simple, and easy to update later.
The PHP include statement helps developers write modular, organized, and maintainable code.
Use include
for optional files.
Use require
for essential files.
Use include_once
and require_once
to prevent duplication.
Always check file paths and handle missing files gracefully.
Whether it’s a blog, e-commerce site, or school portal, include files let you create flexible layouts with consistent design — saving time and reducing errors.
Create a header.php
and footer.php
file, and include them in your main home.php
page.
Create a menu.php
file and include it in three different pages using the include
statement.
Use require
to load a config.php
file that stores database credentials, and display an error if it’s missing.
Create a website layout where the header and footer are included once using include_once
.
Use require_once
to include a functions.php
file and prevent re-inclusion errors.
Demonstrate the difference between include
and require
by trying to include a missing file.
Create a variable $page = "about"
and dynamically include a file named about.php
.
Build a product page that includes header.php
, sidebar.php
, and footer.php
for consistent layout.
Include a file from a subfolder (for example, include partials/header.php
inside index.php
).
Create a main file that includes both a header and footer, and show how editing one file updates all pages automatically.