PHP include, require, include_once, and require_once Compared

Use PHP require when a missing file should stop the script, and include when the script can continue after a warning. Add _once if PHP should skip a file it already included during the current execution.

Compare PHP’s four file inclusion constructs

These are PHP language constructs. The syntax include $file; works without parentheses. PHP reads the file and runs its PHP code in the current execution.

Construct Previously included file If PHP cannot open the file
include Runs again on each call Warns and can continue
include_once Skips a file already included Warns and can continue
require Runs again on each call Raises Error in PHP 8. Stops if unhandled
require_once Skips a file already included Raises Error in PHP 8. Stops if unhandled

The warning behavior assumes ordinary error handling. A project can convert a warning into an exception or handle the Error from require, which can change where execution goes next.

Create a two-file example

Use the PHP 8 command line and create an empty folder. Save part.php and demo.php as UTF-8 files in it. Leave missing.php absent for the failure tests.

Save the first block as part.php. It prints one line with part every time it runs.

<?php
echo "part", PHP_EOL;

Save the next block as demo.php in the same folder. The first argument picks the construct. Pass missing as the second argument to test a nonexistent file.

<?php
$mode = $argv[1] ?? 'include';
$file = __DIR__ . '/part.php';

if (($argv[2] ?? '') === 'missing') {
    $file = __DIR__ . '/missing.php';
}

switch ($mode) {
    case 'include':
        include $file;
        include $file;
        break;
    case 'include_once':
        include_once $file;
        include_once $file;
        break;
    case 'require':
        require $file;
        require $file;
        break;
    case 'require_once':
        require_once $file;
        require_once $file;
        break;
    case 'mixed':
        include_once $file;
        include $file;
        break;
    default:
        fwrite(STDERR, "Unknown mode" . PHP_EOL);
        exit(2);
}

echo "end", PHP_EOL;

Open a terminal in that folder and run these commands individually. Each command runs PHP separately.

php demo.php include
php demo.php include_once
php demo.php require
php demo.php require_once
php demo.php mixed
php demo.php include missing
php demo.php include_once missing
php demo.php require missing
php demo.php require_once missing

Check repeated and missing-file results

  • include and require print two lines of part, followed by end.
  • include_once and require_once print one part line and then end.
  • mixed prints part twice before end. A later plain include or require can run a file again even if it was previously loaded with _once.
  • With the missing file, both include variants warn but still print end. Both require variants stop at the uncaught Error and never reach end.

The format and destination of the diagnostics depend on the PHP configuration and runtime. Check whether the script reaches end.

Use __DIR__ for local paths

PHP’s __DIR__ constant gives you the directory of the PHP file that contains it. A path like __DIR__ . '/part.php' identifies the file next to the script without relying on your current working directory or the include_path search.

Keep raw request parameters and form input out of inclusion paths. If a user chooses a view, map permitted names to fixed local files defined by your application.

Apply the rules to configuration and templates

For required configuration, have the file return its data and load it once with require. If config.php contains return ['debug' => false];, the calling file can read it as follows.

<?php
$config = require __DIR__ . '/config.php';

$config receives the array. Keep that value for later use. A later require_once of the same file can skip execution, so it will not return the configuration array again.

Use require_once for essential declarations that multiple files may reference. For an optional template, check a fixed path with is_file() before using include. Use plain include if you want to render the same template in multiple places.