How to Include a PHP File with require and __DIR__
As a PHP script grows, you move functions into their own files and load them where they're needed. require loads a file, and __DIR__, the folder of the current file, makes the path reliable whichever folder you run the script from.
Syntax
require __DIR__ . '/<file>.php';
| Part | What it does |
|---|---|
require | Loads and runs the file, and stops the script if it can't. |
__DIR__ | The folder of the file this line is in, without a trailing slash. |
. | Joins the folder and the file name into one path. |
The PHP manual: "require is identical to include except upon failure it will also produce an Error exception". On __DIR__, the magic constants page: "The directory of the file, equivalent to dirname(__FILE__). It has no trailing slash unless it is the root directory."
The Example
The example keeps a greet function in greetings.php and loads it from main.php.
Prerequisites
- PHP for the command line. This guide uses PHP 8.6.0.
Step 1: Write the Two Files
Create greetings.php:

<?php
function greet(string $name): string
{
return "Hello, {$name}!";
}
In the same folder, create main.php:

<?php
require __DIR__ . '/greetings.php';
echo greet('Ada'), "\n";
Step 2: Run It
php main.php
Hello, Ada!

require ran greetings.php, which defined greet, so main.php could call it. The command is the same in zsh, bash and PowerShell.
require or include?
They differ only when the file can't be loaded. I pointed both at a missing file: require stopped the script with Fatal error: Uncaught Error: Failed opening required '…/missing.php', while include printed two warnings and carried on to the next line. Use require for files the script can't work without.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing require 'greetings.php'; without __DIR__ | It depends on the folder you run PHP from, and can fail elsewhere | Start the path with __DIR__ |
| Loading the same file of functions twice | Fatal error: Cannot redeclare function greet() (previously declared in …) | Use require_once when a file may be loaded more than once |
Forgetting the / after __DIR__ | The path runs together, such as …/demogreetings.php | Write __DIR__ . '/greetings.php' |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Warning: require(): Failed to open stream: No such file or directory | The path is wrong | Check the file name and use __DIR__ |
Fatal error: Uncaught Error: Failed opening required | The same missing file, with require stopping the script | Fix the path; the warning above it names the problem |
FAQ
What's require_once?
The same as require, except that PHP skips the file if it has already been loaded, which prevents duplicate definitions.
How do larger projects load files?
With an autoloader, usually the one Composer generates, which loads each class file the first time the class is used.
Conclusion
require __DIR__ . '/file.php'; loads another file relative to the current one, so it works from any folder. Use require for files the script needs, and require_once when a file might be loaded twice.
Additional Resources
- require, in the PHP manual.
- Magic constants, in the PHP manual.
