PHP has four common statements to load another PHP file into the current script: include, require, include_once, and require_once.
The main difference is simple. Use require when the file is essential for the page to work. Use include when the file is optional and the page can continue without it.
The _once versions add one more rule. They load the file only one time during the current request. This helps avoid errors like declaring the same function or class twice.
Quick answer
Use this as a quick rule:
require_once: best for config files, database files, function files, class files, and bootstrap files.require: use when the file must be loaded every time and the page should stop if it is missing.include: use for optional template parts like a banner, sidebar, or small content block.include_once: use for optional files that should not be loaded more than once.
In most application code, require_once is the safest default for important PHP files. It makes failure clear and prevents duplicate loading.
Difference between include and require in PHP
The difference is in what PHP does when the file cannot be loaded.
| Statement | When file is found | When file is missing | Best use |
|---|---|---|---|
include |
Loads the file | Shows a warning and continues the script | Optional files |
require |
Loads the file | Shows a fatal error and stops the script | Required files |
include_once |
Loads the file only once | Shows a warning and continues the script | Optional files that should not repeat |
require_once |
Loads the file only once | Shows a fatal error and stops the script | Config, functions, classes, and bootstrap files |
For example, a header template can be included with include. If it is missing, the page may still show the main content. But a database connection file should use require_once. Without that file, the page cannot work correctly.
The PHP manual explains the same behavior for include and requirephp.ini File. The main idea is to decide whether the file is optional or mandatory.
Simple example with include and require
Let us see a small working example using the downloadable project. This will help you understand how these statements behave in real code.
Project structure:
php_include_require_demo/
│
├── index.php
├── config.php
├── functions.php
└── partials/
├── header.php
└── footer.php
index.php
This is the main file. It loads required and optional files.
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/functions.php';
include __DIR__ . '/partials/header.php';
?>
<h2>Home Page</h2>
<p><?php echo getWelcomeMessage(); ?></p>
<?php include __DIR__ . '/partials/footer.php'; ?>
config.php
<?php
define('APP_NAME', 'Include vs Require Demo');
functions.php
<?php
function getWelcomeMessage() {
return "Welcome to " . APP_NAME;
}
header.php
<header>
<h1>My Demo App</h1>
</header>
footer.php
<footer>
<p>Copyright © Demo</p>
</footer>
What happens in this example
require_onceloadsconfig.phpandfunctions.php. If either file is missing, the script stops immediately.includeloadsheader.phpandfooter.php. If one of them is missing, PHP shows a warning but still renders the rest of the page.__DIR__ensures the correct file path, even if the script is moved or included from another file.
This is a common real-world pattern. Core logic uses require_once, while layout parts use include.
Test it yourself
Try this:
- Rename
header.phpto something else and reload. The page still works but shows a warning. - Rename
config.phpand reload. The page stops with a fatal error.
This clearly shows the difference in behavior.
If you are working with modular layouts, you can also check this guide on including PHP files for more examples.
When to use include, require, include_once, and require_once
Choosing the right statement is important. It affects both reliability and error handling in your application.
Use require_once for core files
Use require_once for files that your application cannot run without.
- Database connection files
- Configuration files
- Common functions
- Class definitions
If these files are missing, stopping the script is the correct behavior. It prevents broken or insecure output.
Use require when repeated loading is expected
Use require if you need to load a file every time and want strict failure handling.
This is less common in modern code because require_once is usually safer.
Use include for optional content
Use include when the file is not critical.
- Header or footer templates
- Sidebar blocks
- Optional UI sections
If the file is missing, the page still loads. Only a warning is shown.
Use include_once to avoid duplication
Use include_once when a file should not be loaded more than once.
This is useful when:
- The file defines functions
- The file contains class declarations
- Duplicate loading may cause errors
Quick practical rule
- Default to
require_oncefor backend logic - Use
includefor UI parts - Avoid mixing them randomly
This simple rule keeps your code predictable and easier to debug.
Common errors and how to fix them
When working with include and require, a few common mistakes can cause confusion. Here are the most frequent ones and how to fix them.
1. “Failed to open stream” warning
This happens when PHP cannot find the file.
Warning: include(header.php): Failed to open stream
Fix:
- Check the file path
- Use
__DIR__for reliable paths
<?php
include __DIR__ . '/partials/header.php';
2. Fatal error with require
If a required file is missing, PHP stops execution.
Fatal error: Uncaught Error: Failed opening required file
Fix:
- Ensure the file exists
- Check file permissions
- Use correct relative or absolute path
3. “Cannot redeclare function” error
This happens when the same file is loaded multiple times.
Fatal error: Cannot redeclare function
Fix:
- Use
include_onceorrequire_once
4. Wrong relative path
Relative paths depend on the current script location. This often breaks when files are included from different folders.
Fix:
<?php
require_once __DIR__ . '/../config.php';
This ensures the path is always correct.
5. Mixing include and require incorrectly
Using include for critical files can lead to hidden bugs. The page may continue with missing logic.
Fix:
- Use
require_oncefor important files - Use
includeonly for optional content
For more details on file handling behavior, you can refer to the official PHP documentation.
Security considerations
File inclusion looks simple, but it can become risky if you are not careful. Many security issues in PHP apps come from unsafe include or require usage.
Never include files from user input
This is the most important rule.
<?php
// Dangerous
include $_GET['page'] . '.php';
An attacker can change the URL and load unintended files. This can lead to Local File Inclusion (LFI) or even Remote File Inclusion (RFI).
Safe approach: use a whitelist.
<?php
$allowedPages = ['home', 'about', 'contact'];
$page = $_GET['page'] ?? 'home';
if (in_array($page, $allowedPages)) {
include __DIR__ . "/pages/$page.php";
} else {
echo "Invalid page";
}
Disable remote file inclusion
Make sure your PHP configuration does not allow remote includes.
allow_url_include = Offallow_url_fopen = Off(if not needed)
This prevents loading files from external URLs.
Use absolute paths
Always prefer safe paths using __DIR__ or full paths.
<?php
require_once __DIR__ . '/config.php';
This avoids path manipulation issues.
Limit file permissions
Ensure your included files are not writable by public users.
- Set proper file permissions
- Avoid placing sensitive files in public directories
Do not expose error messages in production
Include and require errors can reveal file paths.
In production:
- Turn off display errors
- Log errors instead
These small practices make your file inclusion much safer.
Developer FAQ
Is include faster than require?
No meaningful difference in modern PHP. Choose based on behavior, not speed.
Should I always use require_once?
For most backend logic, yes. It prevents duplicate loading and stops execution if something is wrong.
Can I mix include and require in one project?
Yes, but use them consistently. Use require_once for core logic and include for optional UI parts.
What is the best practice for large projects?
Use autoloading with Composer. It replaces manual include and require in many cases. See the Composer autoloading guide for details.
Download source code
You can download the complete working example used in this tutorial.
Download PHP include vs require demo project (ZIP)
The project includes:
- Working example files
- Clean folder structure
- README with setup instructions
This is a simple project you can run locally to test and understand how each statement behaves.
Conclusion
The difference between include and require is small, but important.
require stops execution when a file is missing. include shows a warning and continues. The _once versions prevent duplicate loading.
In real projects, a simple pattern works well:
- Use
require_oncefor config, functions, and classes - Use
includefor templates and optional UI parts
This keeps your code safe, predictable, and easier to debug.
If you follow this rule and use safe paths like __DIR__, you will avoid most common errors.
For related reading, you can check this guide on how to include PHP files which shows more practical usage.
These are not functions, but statements, thus the parentheses around the file name are not necessary.
Yes Pernikar, thats right and I agree with you.
Thanks A lot I understood
Welcome!