include and require
A PHP program that grows past one file is split with four statements: include, require and their _once forms. They differ in what happens when the file is missing and in how often the same file may run.
include is optional
The include statement loads another file, and a missing file raises only a warning while the page keeps running. That makes it right for optional blocks such as sidebars and banners. The full comparison lives at require, require_once, include, include_once.
The file sidebar.php:
<?php echo "sidebar", PHP_EOL;
The page that includes it, index.php:
<?php include(__DIR__ . "/sidebar.php"); echo "page continues", PHP_EOL;
php index.php
sidebar
page continues
The line of the included file runs where the include sits, so the sidebar prints first. If the file were missing, the warning would appear in place of the first line and the second line would still print.
require is mandatory
The require statement also loads another file, but a missing file stops the page with a fatal error. Use it for code the page cannot work without: configuration, helpers, and database connections.
The file config.php:
<?php $appName = "shop";
The page that requires it, index.php:
<?php require(__DIR__ . "/config.php"); echo "app: " . $appName, PHP_EOL;
php index.php
app: shop
The variable the included file defines is in the scope of the include, so the page uses $appName as if it had assigned it itself.
The _once variants
The include_once and require_once variants load a file at most one time per request. A second request for the same resolved path is skipped, which prevents fatal Cannot redeclare function errors when two files share one helper.
The file helpers.php:
<?php function greeting(string $name): string { return "hello " . $name; }
The page that asks for it twice, index.php:
<?php require_once(__DIR__ . "/helpers.php"); require_once(__DIR__ . "/helpers.php"); echo greeting("Ada"), PHP_EOL; echo "loaded once", PHP_EOL;
php index.php
hello Ada
loaded once
The second require_once does nothing, so the function is defined once and the page keeps running; a plain require there would stop it with a redeclaration error.
Paths: __DIR__ wins
A relative path is looked up in the include path first, and that path starts with the working directory, so a file next to the caller can lose to one in the folder the program was started from. Prefixing with __DIR__ names the file next to the current file and works from everywhere. For optional modules that may be absent, guard with is_file() as shown in the require fallback pattern.
The file lib/db.php:
<?php function connect(): string { return "connected"; }
The page that checks the path first, index.php:
<?php $path = __DIR__ . "/lib/db.php"; var_dump(is_file($path)); require($path); echo connect(), PHP_EOL;
php index.php
bool(true)
connected
The checked path points at the file beside the page whatever folder the command runs from, because __DIR__ is the folder of index.php itself.
Exercises
- Rename sidebar.php in the include example and run the page. Which line prints, and what does the warning say?
- Rename config.php in the require example and run the page. How far does the program get, and which error class appears?
- Replace the second require_once in the helpers example with a plain require and run it. What happens, and why?
- Print $path before the var_dump in the last example and run the program from another folder. Which part of the path changes, and which does not?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What happens when an
includenames a file that does not exist?Answer
A warning is raised and the page continues
include is the optional form: the missing file is a warning, and execution goes on after the statement.
-
What happens when a
requirenames a file that does not exist?Answer
The page stops with a fatal error
require is the mandatory form, so a missing file ends the request with a fatal error.
-
What is the difference between
includeandinclude_once?Answer
include_onceloads the same path at most once per requestThe once form remembers the resolved path and skips a repeated include of it.
-
What does
__DIR__contain?Answer
The folder of the file in which it is written
__DIR__ is a compile-time constant holding the directory of the file, which is why it survives a move of the entry point.
-
What does this program print?
<?php var_dump(__DIR__ === dirname(__FILE__));Answer
bool(true)
__DIR__ and dirname(__FILE__) name the same folder, so the strict comparison of two identical strings is true.
-
What does this program print?
<?php var_dump(is_file(__FILE__));Answer
bool(true)
__FILE__ is the path of the running file, and that file exists while it runs.
-
What does this program print?
<?php var_dump(is_file(__DIR__ . "/missing.php"));Answer
bool(false)
is_file answers a question and never warns, so a missing file gives false.
-
What does this program print?
<?php $path = __DIR__ . "/side.php"; file_put_contents($path, "<?php echo 'side';"); include($path);Answer
side
include runs the code of the file, so the echo inside it prints side.
-
Why is
__DIR__ . "/config.php"safer than"config.php"in an include?Answer
It names the file next to the current file, whatever folder the program runs from
A bare relative path is searched from the working directory and the include path, so another file can win; __DIR__ fixes the folder.
-
Which pair of statements both stop the program when the file is missing?
Answer
requireandrequire_onceBoth require forms treat a missing file as fatal; both include forms only warn.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev