PHP: What is the difference between require and include?

PHP has no fewer than four constructs for including a file. Behind them, however, there are only two independent decisions.

The two decisions

  1. require or include — what happens if the file is missing?
  2. with or without _once — may the same file be included more than once?

Everything else is identical. The four combinations give you the four constructs.

require or include

The difference only shows up when something goes wrong. Let us include a file that does not exist.

With include:

Warning: include(gibt-es-nicht.php): Failed to open stream:
No such file or directory in /app/mit-include.php on line 2

Nach include: das Skript laeuft weiter
(exit code: 0)

With require:

Warning: require(gibt-es-nicht.php): Failed to open stream:
No such file or directory in /app/mit-require.php on line 2

Fatal error: Uncaught Error: Failed opening required 'gibt-es-nicht.php'
in /app/mit-require.php:2
(exit code: 255)

include emits a warning and carries on. require aborts. The exit code shows the difference clearly: 0 in one case, 255 in the other.

From this follows the rule of thumb: require for anything you cannot go on without — classes, configuration, the autoloader. include only for optional things, such as an advertising banner or a template fragment whose absence does not break the page.

When in doubt, require. A half-loaded application cheerfully carrying on is harder to debug than a clean abort.

With or without _once

The _once variants include a file only if that has not already happened:

1. include:            [zaehler.php wurde eingebunden]
2. include (nochmal):  [zaehler.php wurde eingebunden]
3. include_once:       (nothing)
4. include_once:       (nothing)

Line 3 is the interesting one. That is the first include_once, yet nothing happens. The reason: the check applies to the file, not to the construct. It had already been loaded twice with include, so it counts as included.

Why this matters

As long as a file only outputs something, including it repeatedly is harmless. As soon as it defines a function or a class, it is a fatal error:

<?php
  include 'lib.php';
  include 'lib.php';   // defines begruessung() a second time
?>
Fatal error: Cannot redeclare function begruessung()
(previously declared in /app/lib.php:3)

That is exactly what the _once variants are for. So for files containing functions or classes, use require_once.

Included files can return something

This is a detail many people do not know, and the basis for a very widespread pattern. An included file can return a value with return:

<?php
  // config.php
  return [
    'host' => 'localhost',
    'port' => 3306,
  ];
?>

And at the call site:

<?php
  $config = include 'config.php';
  echo $config['host'];   // localhost
?>
Aus nur-array.php: array (
  'host' => 'localhost',
  'port' => 3306,
)

That is considerably cleaner than setting a global variable in the included file and hoping nobody overwrites it. Practically every modern framework reads its configuration this way.

Do I even still need this today?

Honestly: hardly. Since autoloading exists, you no longer include classes by hand. In a project using Composer there is usually exactly one require in all of the application code:

<?php
  require_once __DIR__ . '/vendor/autoload.php';
?>

The autoloader pulls in everything else on demand. __DIR__ matters here so that the path is correct regardless of the working directory.

What remains are the configuration files above and the occasional template fragment.

Summary

  • require aborts, include carries on — exit code 255 versus 0.
  • When in doubt, require.
  • Use _once whenever the file defines functions or classes.
  • include_once checks the file, not which construct was used.
  • An included file can return a value with return — the usual pattern for configuration.
  • With Composer you normally need a single require_once.

About Netcup (advertisement)

The German host Netcup offers, among other things, affordable and powerful web hosting packages, KVM-based root servers and dedicated servers. With our voucher codes you can save even more (6€ off your first order, 30% off all KVM-based root servers, ...).