require
In PHP, the "require" keyword is used to include and evaluate external PHP files within your code. In this article, we'll explore the syntax and usage of the
The PHP require Statement
In PHP, require takes the contents of another PHP file, inserts them at the point where require appears, and evaluates them as if they were written there. It is how PHP projects split code across multiple files: configuration, helper functions, class definitions, and templates each live in their own file and are pulled in with require.
The key thing that sets require apart from its sibling include is how it reacts to a missing file. If the target file cannot be found, require stops the script with a fatal error, while include only emits a warning and keeps going. That makes require the right choice for files your script cannot run without.
This guide covers the syntax, runnable examples, the require vs include difference, when to reach for require_once, and the gotchas that trip people up.
Syntax
require is a language construct, not a function, so you can write it with or without parentheses:
require '/path/to/file.php';
require('/path/to/file.php'); // also valid, but parentheses are unnecessaryIn practice you almost always want paths to be relative to the current script rather than to PHP's working directory. The __DIR__ magic constant holds the directory of the file it appears in, so building paths from it keeps require working no matter where the script is run from:
require __DIR__ . '/config.php';A runnable example
The snippet below creates a small helper file, requires it, and then calls the function it defines. You can paste it into a single .php file and run it — the helper is written to the same directory at runtime so the example is self-contained.
<?php
// Create a helper file next to this script.
file_put_contents(__DIR__ . '/greet.php', <<<'PHP'
<?php
function greet($name) {
return "Hello, $name!";
}
PHP);
// Pull the helper in. After this line greet() is available.
require __DIR__ . '/greet.php';
echo greet('Ada'); // Hello, Ada!Output:
Hello, Ada!Once a file is required, everything it declares — functions, classes, and any top-level variables — becomes part of the current scope, exactly as if you had typed it inline.
require vs include
Both statements load and evaluate a file. The only behavioral difference is what happens when the file is missing or unreadable:
| Behavior on failure | require | include |
|---|---|---|
| Error emitted | Fatal error (E_COMPILE_ERROR) | Warning (E_WARNING) |
| Script execution | Stops immediately | Continues |
| Typical use | Files the app cannot run without | Optional or non-critical files |
Use require for things like a database-connection file or an autoloader, where continuing without them makes no sense. Reach for include only when a missing file should be tolerated — for example, an optional theme override.
require_once: avoid loading twice
Requiring the same file twice causes problems: redeclaring a function or class is itself a fatal error. require_once solves this by remembering which files have already been loaded and skipping any repeat request.
<?php
require_once __DIR__ . '/User.php'; // loads the file
require_once __DIR__ . '/User.php'; // already loaded — does nothingAs a rule of thumb, use require_once for files that define things (classes, function libraries) and plain require for files you intentionally run more than once, such as a template rendered in a loop. The same once/non-once distinction exists for include as include_once.
Common patterns
A shared configuration file loaded at the top of an entry script:
<?php
require __DIR__ . '/config.php';
// $dbHost, $dbUser, etc. defined in config.php are now available.
$pdo = new PDO("mysql:host=$dbHost", $dbUser, $dbPass);A required file can also return a value, which require evaluates to. This is a clean way to load a config array:
// config.php
<?php
return [
'host' => 'localhost',
'port' => 3306,
];// index.php
<?php
$config = require __DIR__ . '/config.php';
echo $config['port']; // 3306Gotchas
- Build paths from
__DIR__, not relative strings.require 'config.php'resolves against PHP's current working directory, which can differ from the script's location and silently break under different web servers or CLI invocations. requireruns at the point it appears. Putting it inside a function (as in the example above) defers loading until that function is called, and limits any top-level variables in the file to that function's scope.- Prefer an autoloader for classes. Manually requiring dozens of class files is error-prone. Modern projects use Composer's
vendor/autoload.php(loaded once withrequire) and let PSR-4 autoloading pull in classes on demand.
Conclusion
require is the foundation of code organization in PHP: it splits a program into focused files and loads the ones a script depends on, failing loudly if any are missing. Build paths from __DIR__, choose require_once for definitions to avoid double-loading, and lean on include only when a missing file is acceptable. For libraries of classes, a Composer autoloader is the modern replacement for hand-written require lists.