PHP
Web Development
Programming
Code Functions
Syntax Differences

Difference between require, include, require_once and include_once?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

PHP gives you four related ways to load another file: include, require, include_once, and require_once. They all evaluate the target file, but they differ in two important behaviors: what happens when the file is missing, and whether PHP allows the same file to be loaded more than once.

include Versus require

The first distinction is error handling.

  • 'include emits a warning if the file cannot be loaded, then the script continues.'
  • 'require throws a fatal error and stops execution.'

That difference determines when each keyword is appropriate. If the file is optional, include is acceptable. If the file is essential for bootstrapping the application, require is the safer choice.

php
1<?php
2
3include 'optional-banner.php';
4echo "The script still runs after include.\n";
5
6require 'config/database.php';
7echo "This line runs only if the config file exists.\n";

In a real application, config/database.php is not optional. If it cannot be loaded, continuing the request usually creates more confusing failures later, so require is the right fit.

What _once Adds

The _once variants do the same work but also prevent duplicate inclusion of the same file during the current request.

  • 'include_once is the warning version with duplicate protection.'
  • 'require_once is the fatal version with duplicate protection.'

This matters most for files that declare functions, classes, interfaces, or constants. Loading the same file twice can trigger redeclaration errors.

php
1<?php
2
3require_once 'bootstrap.php';
4require_once 'bootstrap.php';
5
6echo "bootstrap.php was evaluated only once.\n";

Without _once, the second load may fail if bootstrap.php defines functions or classes that PHP now sees for a second time.

A Practical Rule of Thumb

Use require_once for shared bootstrap files, autoload setup, and library definitions. That is the most common default in modern PHP applications because those files are required and should not be loaded twice.

Use include or include_once for optional presentation fragments, such as a sidebar, footer, or environment-specific helper that can be skipped safely.

Here is a realistic example:

php
1<?php
2
3require_once __DIR__ . '/vendor/autoload.php';
4require_once __DIR__ . '/config/app.php';
5
6include __DIR__ . '/views/header.php';
7include __DIR__ . '/views/flash-message.php';
8
9echo "<main>Page content</main>";
10
11include __DIR__ . '/views/footer.php';

The autoloader and app configuration are mandatory, so require_once makes sense. The view fragments are part of rendering and can be handled differently depending on how strict you want the application to be.

Performance and Readability

People sometimes worry about _once being slower because PHP has to track whether the file was already loaded. That overhead exists, but in normal application code it is rarely the deciding factor. Correctness and clarity matter more than a tiny difference in include bookkeeping.

If you know a file absolutely must be loaded exactly once, choose the form that communicates that intent. Future maintainers learn a lot from those choices.

Choosing the Right One

You can think of the four options as two separate decisions:

  1. Is the file mandatory
  2. Should duplicate inclusion be blocked

That gives this practical mapping:

  • Mandatory and single-load: require_once
  • Mandatory and repeated inclusion allowed: require
  • Optional and single-load: include_once
  • Optional and repeated inclusion allowed: include

Most of the time, the real choice is between require_once and include.

Common Pitfalls

The most common mistake is using include for required configuration files. The page may continue running after the warning, but now the application is in a broken partial state that is harder to debug than a clean fatal error.

Another mistake is using plain require or include for files that define functions or classes. If that file is loaded twice through different paths, PHP can crash with redeclaration errors. require_once avoids that class of bug.

Path handling is another source of trouble. Relative paths depend on the current working directory and entry point. Using __DIR__ makes file loading more reliable.

Finally, do not treat _once as a substitute for application structure. If files are included from many directions without a clear bootstrap flow, the codebase becomes fragile even if _once masks some duplicate loads.

Summary

  • 'include warns on failure and continues execution.'
  • 'require fails hard when the file is missing.'
  • '_once prevents the same file from being evaluated multiple times in one request.'
  • 'require_once is the common choice for config, bootstrap, and autoload files.'
  • 'include or include_once is better for optional templates or noncritical fragments.'

Course illustration
Course illustration

All Rights Reserved.