Include & Require
Learn how to split PHP code across multiple files using include, require, and their _once variants.
Introduction
So far, every example you have written has lived in a single file. Real PHP projects, however, quickly grow into dozens or hundreds of files — separate files for database connections, reusable functions, headers, footers, and page-specific logic.
PHP lets you pull the contents of one file into another using include and require. This lesson covers both statements, how they differ, their safer _once variants, and a reliable way to reference file paths no matter where a script is run from.
- Why splitting code across multiple files is useful.
- How include and require pull one file's contents into another.
- The key difference between include and require when a file is missing.
- Why include_once and require_once prevent files from being loaded twice.
- How to use __DIR__ to build reliable file paths.
Why Split Code Across Files?
Instead of writing every function, every database connection, and every page of HTML into one giant script, PHP developers organize related code into separate files and combine them where needed.
Reusability
Write a function or a header once, and reuse it across every page that needs it.
Organization
Keep database logic, configuration, and page templates in clearly separated files.
Easier Maintenance
Update shared code — like a navigation menu — in one place instead of every page.
Team-Friendly
Different developers can work on different files without constantly colliding.
The include Statement
The include statement loads and evaluates the specified file at the point where it is called. Any variables, functions, or classes defined in that file become available in the including script.
<?php
$greeting = "Hello from greeting.php!";
?><?php
include "greeting.php";
echo $greeting . "\n";
?>Hello from greeting.php!If the included file does not exist, include emits a warning but PHP continues executing the rest of the script.
<?php
include "does-not-exist.php";
echo "The script kept running after the missing include.\n";
?>Warning: include(does-not-exist.php): Failed to open stream
The script kept running after the missing include.The require Statement
The require statement works the same way as include, but treats a missing file far more seriously. If the required file cannot be found, PHP raises a fatal error and stops executing the script immediately.
<?php
require "does-not-exist.php";
echo "This line will never run.\n";
?>Fatal error: require(): Failed opening required 'does-not-exist.php'If a file is absolutely essential for the rest of the script to work — like a database connection file — require is the safer choice. There is no point continuing to run a script that depends on code that failed to load.
include vs require
Both statements load and run a file's contents in the exact same way. The only real difference is how they react when the file cannot be found.
include
- Missing file produces a warning
- Script execution continues
- Best for optional or non-critical files
- Example: an optional sidebar widget
require
- Missing file produces a fatal error
- Script execution stops immediately
- Best for files the script cannot run without
- Example: a database connection file
A simple rule of thumb: use require for anything the rest of the script truly depends on, and include for anything that is nice to have but not essential.
include_once & require_once
If the same file gets included twice — often by accident, through a chain of other includes — PHP will try to redeclare any functions or classes in it, causing a fatal "already declared" error.
<?php
function sayHello() {
echo "Hello!\n";
}
?><?php
include "functions.php";
include "functions.php"; // Loaded a second time
sayHello();
?>Fatal error: Cannot redeclare sayHello()include_once and require_once solve this by keeping track of which files have already been loaded, and silently skipping a file that is included again.
<?php
include_once "functions.php";
include_once "functions.php"; // Skipped - already loaded
sayHello();
?>Hello!For files that define functions or classes, always prefer include_once / require_once. It is very easy for the same file to end up included multiple times once your project grows to include several other files that each include their own dependencies.
Using __DIR__ for Reliable Paths
A common source of bugs is using a relative path like "config.php" in an include statement. Whether that path resolves correctly depends on which directory the script was run from — and that can change depending on how the page was requested.
PHP provides the magic constant __DIR__, which always evaluates to the absolute path of the directory containing the current file. Combining it with the included file's relative path produces a path that works no matter where the script is executed from.
<?php
// Unreliable - depends on the current working directory
include "config.php";
// Reliable - always relative to this file's own location
require_once __DIR__ . "/config.php";
?><?php
// Inside /app/pages/dashboard.php
require_once __DIR__ . "/../includes/db.php";
require_once __DIR__ . "/../includes/functions.php";
echo "Dashboard dependencies loaded successfully.\n";
?>Dashboard dependencies loaded successfully.Common Mistakes
- Using require for critical files but not noticing (or ignoring) the fatal error when a typo breaks the path.
- Using plain include/require repeatedly for the same file and hitting "cannot redeclare" errors.
- Relying on relative paths without __DIR__, which can break depending on how a script is invoked.
- Using include for essential files, letting the script continue to run with missing functionality and confusing errors later.
Best Practices
- Use require or require_once for anything the script cannot function without.
- Use include or include_once for genuinely optional content.
- Default to the _once variants for any file defining functions, classes, or constants.
- Always build include/require paths with __DIR__ instead of bare relative paths.
Frequently Asked Questions
Can I use include or require inside a function?
Yes. Any variables, functions, or classes defined in the included file become available according to the scope in which the include or require was called.
Does it matter whether I write include "file.php"; or include("file.php");?
No, both forms work. include and require are language constructs, not real functions, so the parentheses are optional.
Is there a performance difference between include and require?
No meaningful difference. The choice between them should be based on whether the file is essential, not on performance.
What happens to variables set before an include statement?
They remain available inside the included file, since the included code runs in the same scope as the line that included it.
Should I always use the _once variants everywhere?
For files with function/class definitions, yes. For a page-specific template you intentionally include multiple times in a loop, plain include is fine.
Key Takeaways
- include and require both load and execute the contents of another PHP file.
- include produces a warning and continues if the file is missing; require produces a fatal error and stops.
- include_once and require_once prevent a file from being loaded and executed more than once.
- __DIR__ gives the absolute directory of the current file, making include/require paths reliable everywhere.
- Splitting code across files improves reusability, organization, and long-term maintainability.
Summary
include and require let you break a PHP project into organized, reusable files instead of one massive script. The choice between them comes down to how critical the file is: require for anything essential, include for anything optional.
In this lesson, you learned the difference between include and require, how the _once variants avoid duplicate-declaration errors, and how __DIR__ keeps your file paths reliable. Next, you will learn about superglobals — PHP's built-in arrays for accessing request data anywhere in your script.
- You can split PHP code across multiple files using include and require.
- You understand the difference in how each handles a missing file.
- You know when to reach for include_once and require_once.
- You can build reliable file paths using __DIR__.