Estimated reading time: 5 min
How to Add and Use Include in PHP. A Complete Beginner Guide. Learn how to add and use include
in PHP to modularize your code, reuse functions, and maintain cleaner files. This step-by-step tutorial shows you real examples, benefits, and best practices.
Introduction to PHP include
If you’re just starting with PHP or building your first website with custom code, you’ve probably seen or heard about the include
statement. It’s a powerful feature in PHP that allows you to reuse code from other files, keeping your projects organized and efficient.
In this article, you’ll learn:
- What is
include
in PHP? - How to use
include
with real-world examples. - The difference between
include
,require
,include_once
, andrequire_once
. - Best practices for modularizing PHP code.
Let’s get started.
What is include
in PHP?
In PHP, include
is a statement used to insert the contents of one PHP file into another PHP file before the server executes it.
Syntax:
include 'filename.php';
This allows you to keep common code like headers, footers, functions, or configurations in separate files and include them wherever needed.
Why Use include
?
Using include
is all about code reusability and better organization. Imagine you have a large website with multiple pages, and each page shares a common header and footer. Instead of copying and pasting the same HTML or PHP code on every page, you can simply include the same header and footer files.
Benefits:
- Reduces code duplication
- Simplifies updates (change one file, affects all)
- Cleaner and more maintainable code
- Better for teamwork and modular development

Basic Example of include
Let’s create a simple example.
1. Create a file called header.php
:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
2. Then create footer.php
:
<footer>
<p>Copyright © 2025</p>
</footer>
</body>
</html>
3. Now create your main page index.php
:
<?php include 'header.php'; ?>
<main>
<p>This is the homepage content.</p>
</main>
<?php include 'footer.php'; ?>
When you load index.php
, it will display the header, main content, and footer — all combined.
include
vs require
You might also see require
used in PHP. So what’s the difference?
include
: If the file is missing, PHP will throw a warning but continue executing the rest of the code.require
: If the file is missing, PHP will throw a fatal error and stop the script.
Example of include
:
include 'nonexistent.php';
echo "Hello World!"; // Will still be executed
Example of require
:
require 'nonexistent.php';
echo "Hello World!"; // Will NOT be executed
Use require
when the file is essential for the script to run, like config or database files.
include_once
and require_once
Sometimes you don’t want to include a file multiple times. That’s where include_once
and require_once
come in.
Use Case:
include_once 'functions.php';
include_once 'functions.php'; // Will not be included again
This avoids “function already declared” errors if a file is accidentally included multiple times.
Using Paths in include
PHP looks for the included file relative to the current script location. You can use relative or absolute paths.
Relative path:
include 'includes/config.php';
Absolute path (better for larger projects):
include $_SERVER['DOCUMENT_ROOT'] . '/includes/config.php';
You can even define a constant like BASE_PATH
for consistency:
define('BASE_PATH', $_SERVER['DOCUMENT_ROOT'] . '/myproject/');
include BASE_PATH . 'includes/config.php';
Dynamic Includes (Use with Caution)
You can also dynamically include files based on variables, but this can be risky if not properly sanitized.
$page = $_GET['page'];
include $page . '.php'; // risky if no validation
Always sanitize user input and avoid dynamic includes unless you know what you’re doing.
Common Use Cases for include
- Header and Footer – Reuse on all pages.
- Configuration Files – Store constants, DB credentials, etc.
- Functions Files – Define and reuse custom functions.
- Navigation Menus – Centralize menu markup or logic.
- Reusable Components – Cards, widgets, etc.
Best Practices
- Use consistent naming (e.g.,
header.php
,footer.php
,functions.php
) - Group includes in a folder like
/includes
or/partials
- Prefer
require
for critical files like configs - Use
include_once
for function or class files - Always validate and sanitize any user input if using dynamic includes
- Organize your includes logically — avoid deeply nested includes
Real World: Include in WordPress Themes
If you’re building a custom WordPress theme, you’ll often use get_header()
, get_footer()
, and get_template_part()
— these are essentially WordPress-friendly include
functions.
But in custom projects or plugins, you can still use raw PHP include
like this:
include plugin_dir_path(__FILE__) . 'includes/custom-settings.php';
Conclusion
The include
statement in PHP is a fundamental building block for writing clean, modular, and maintainable code. Whether you’re a beginner learning to reuse a simple header or an advanced developer organizing a complex application, include
is your friend.
Remember the key takeaways:
- Use
include
for optional files. - Use
require
for essential files. - Use
*_once
versions to avoid multiple inclusions. - Always structure your project with clarity.
Now that you know how to use include
, try refactoring one of your messy PHP pages — and feel the difference!
🏷️ Tags: php, include, require, web development, php tutorial, php beginner, php functions, code reuse, wordpress php, php tips
Discover more from HelpZone
Subscribe to get the latest posts sent to your email.