require, include, require_once, include_once in PHP: Full Breakdown with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

require, include, require_once, include_once in PHP: What to Choose and Why It Matters?

\n\n

Have you ever caught yourself thinking: "What exactly is the difference between require and include in PHP? And why were those strange _once versions invented?" — If so, you are not alone. This question is among the top most popular on Stack Overflow (thousands of views and dozens of votes). And that's no coincidence.

\n\n

Every day, thousands of PHP developers — from beginners to experienced — use these functions. But many use them on autopilot, without thinking about the consequences. And that's a mistake. The wrong choice can lead to fatal errors, code duplication, or, even worse, to non-obvious bugs that only appear on the production server.

\n\n

Today, we will once and for all figure out how require, include, require_once, and include_once differ. You will learn not only the theory but also see live code examples that you can run immediately. Let's go!

\n\n

1. The Basics: Why Include Files at All?

\n\n

Before discussing the differences, let's remember why we do this. In real projects, PHP code is not written in one giant file. That would be a maintenance nightmare. Instead, code is split into modules:

\n\n
    \n
  • Configuration — config.php
  • \n
  • Functions — functions.php
  • \n
  • Templates — header.php, footer.php
  • \n
  • Classes — User.php, Database.php
  • \n
\n\n

And this is exactly where we need require and include. They literally "paste" the contents of one file into another. PHP processes the included file as if its code were written right in that spot.

\n\n

2. require vs include: The Main Difference — Error Handling

\n\n

This is the most important point. Remember it once and for all.

\n\n

include — Soft Inclusion

\n\n

include attempts to include a file. If the file does not exist or an error occurs, PHP issues a Warning, but continues executing the script.

\n\n
<?php\n// include.php\necho "This is include. ";\ninclude 'non_existent_file.php'; // Will trigger a Warning\necho "The script continues working!";\n?>\n
\n\n

Result:

\n
This is include. Warning: include(non_existent_file.php): failed to open stream...\nThe script continues working!
\n\n

See? The script didn't crash; it moved on. This is useful when including a file is optional. For example, if you are including a widget that is not critical for the page to function.

\n\n

require — Hard Inclusion

\n\n

require does the same thing, but with one difference: if the file is not found, PHP issues a Fatal Error and immediately stops the script.

\n\n
<?php\n// require.php\necho "This is require. ";\nrequire 'non_existent_file.php'; // Will trigger a Fatal Error\necho "This code will never execute!";\n?>\n
\n\n

Result:

\n
This is require. Fatal error: Uncaught Error: Failed opening required 'non_existent_file.php'...
\n\n

The script died. That's it. Silence from here on.

\n\n

When to Use What?

\n
    \n
  • require — for critically important files: database configuration, core classes, router. If they are missing, the site cannot function.
  • \n
  • include — for optional blocks: templates, widgets, advertising banners. If the file is not included, the page will simply display without that block.
  • \n
\n\n

3. require_once and include_once: Protection Against Duplication

\n\n

Now imagine a situation. You have a file config.php that declares constants and functions. You p

Blogs

Book Recommendations