PHP require Statement
last modified November 25, 2026
The PHP require statement evaluates another PHP file as part of the current script. When the file cannot be opened, the script stops, which makes require the right choice for libraries, configuration, and bootstrap code that the program cannot run without.
This article shows require with a function library and a configuration file, the value a required file can return, the failure behaviour, and the scope in which included code runs.
Basic definitions
require is a language construct that evaluates another PHP file as part of the current script. It reads the file, compiles it, and executes it at the point of the statement, so the included code can define functions, classes, constants, and variables that the rest of the script uses.
The construct behaves like include except when the file cannot be opened. include reports a warning and continues, while require reports the warning and then throws an Error, which ends the script with exit status 255 unless something catches it. That difference is the whole point of the keyword: it marks a file the program cannot run without.
require is an expression, so its result can be assigned. The value is whatever the required file returns, or the integer 1 when the file has no return statement. A bare filename is looked up in include_path, then in the directory of the calling script, and finally in the current working directory. A path that is absolute or starts with ./ or ../ skips include_path and is resolved directly. __DIR__, the directory of the current file, removes the remaining ambiguity.
Every call executes the file again. Requiring a file that declares a function a second time is a fatal error, which is why a library is usually loaded once. The require_once variant, covered in the next article, keeps track of loaded files and skips repeats.
Requiring a function library
The first example loads a small library. The library file and the script that uses it are separate files in the same directory.
<?php
declare(strict_types=1);
function average(array $values): float
{
return array_sum($values) / count($values);
}
The library declares one function and nothing else. Running it on its own produces no output; its only purpose is to be loaded by another script.
<?php declare(strict_types=1); require __DIR__ . '/math_lib.php'; $scores = [12, 17, 9, 16]; echo "Average score: ", average($scores), "\n";
totals.php builds the path as __DIR__ . '/math_lib.php', so the require works from any working directory. The construct runs before the call to average, which makes the function available for the rest of the file. Functions declared by an included file are global, so they are visible outside the statement as well.
$ php totals.php Average score: 13.5
The value returned by require
A required file does not have to define variables. It can return a value, and require evaluates to that value. Configuration is the usual case.
<?php
declare(strict_types=1);
return [
'currency' => 'EUR',
'tax' => 0.2,
];
The file returns an array and declares nothing in the global scope. Each inclusion produces a fresh array, and the caller decides where to store it.
<?php declare(strict_types=1); $settings = require __DIR__ . '/settings.php'; $net = 250.0; $total = $net * (1 + $settings['tax']); echo $net, " ", $settings['currency'], " plus tax = ", $total, "\n";
The assignment $settings = require __DIR__ . '/settings.php' stores the returned array in a local variable. Nothing leaks into the global scope, so the file can be required inside a function or a test without name clashes. Constants and global variables, as used by the include article, would work too, but a returned array is easier to override in a test and cannot be changed by accident from another file.
$ php invoice.php 250 EUR plus tax = 300
A missing file stops the script
The difference between the two constructs appears when the file does not exist. The next script tries include first and require second, on the same missing path.
<?php declare(strict_types=1); include __DIR__ . '/nope.php'; echo "The script continues after include.\n"; require __DIR__ . '/nope.php'; echo "This line is not printed.\n";
PHP first reports that it cannot open the stream and that the inclusion failed, which is the warning produced by include; the script then continues and prints its next line. For require the same two warnings are followed by Uncaught Error: Failed opening required ..., the script stops, and the exit status is 255. The final echo never runs. A require should therefore name a file that must exist for the program to work: a bootstrap, a library, or a configuration file.
$ php broken.php
Warning: include(/app/nope.php): Failed to open stream: No such file or directory in /app/broken.php on line 5
Warning: include(): Failed opening '/app/nope.php' for inclusion (include_path='.:/usr/local/lib/php') in /app/broken.php on line 5
The script continues after include.
Warning: require(/app/nope.php): Failed to open stream: No such file or directory in /app/broken.php on line 8
Fatal error: Uncaught Error: Failed opening required '/app/nope.php' (include_path='.:/usr/local/lib/php') in /app/broken.php:8
Stack trace:
#0 {main}
thrown in /app/broken.php on line 8
Where the included code runs
A required file runs in the scope of the statement that requires it. At the top level its variables become global, but inside a function they belong to that call.
<?php declare(strict_types=1); $appName = "Zeta"; $version = "2.4";
The file assigns two variables and declares no functions or classes.
<?php
declare(strict_types=1);
function loadAppName(): string
{
require __DIR__ . '/vars.php';
return $appName;
}
echo loadAppName(), "\n";
echo isset($appName) ? "global set\n" : "global not set\n";
require __DIR__ . '/vars.php';
echo "global: $appName\n";
The first call requires the file inside loadAppName, so $appName exists only during that call and the global scope has no such variable. The top-level require then creates the global variable, which is why the last line can print it. Functions and classes do not follow this rule: they are declared globally wherever the require appears.
$ php scope.php Zeta global not set global: Zeta
require_once
A file that declares a function or a class cannot be required twice, because the second inclusion would redeclare the name and PHP stops with a fatal error. The require_once construct remembers the absolute path of every file it has loaded and skips a file that is already included, even when it is reached through a different relative path.
The construct is covered in PHP require_once statement. For class files, an autoloader is usually a better solution than a long list of requires; the Composer and autoloading tutorial shows how Composer generates one.
Best practices
require is for hard dependencies, and the path deserves as much care as the content of the file.
- Use
requirefor files the script cannot run without, andincludeonly for optional fragments. - Build paths with
__DIR__instead of relying on the current working directory. - Never build an include path from unvalidated user input; it allows an attacker to load an arbitrary file.
- Prefer a file that returns an array over one that defines global variables or constants.
- Load class files through an autoloader rather than a list of requires.
- Use
require_oncewhen the same library may be reached through several paths.
Source
This article covered the PHP require statement: loading a function library, the value returned by the required file, the fatal error raised by a missing file, and the scope of the included code. Continue with PHP require_once statement for repeated inclusion, or compare with PHP include for optional files.
Author
List all PHP tutorials.