Skip to main content

How to Include a PHP File with require and __DIR__

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
require __DIR__ . '/<file>.php';
PartWhat it does
requireLoads 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:

greetings.php, which defines greet(string $name): string returning Hello, name!

greetings.php
<?php

function greet(string $name): string
{
return "Hello, {$name}!";
}

In the same folder, create main.php:

main.php, which requires DIR . '/greetings.php' and echoes greet('Ada').

main.php
<?php

require __DIR__ . '/greetings.php';

echo greet('Ada'), "\n";

Step 2: Run It​

Terminal
php main.php
Output
Hello, Ada!

A zsh terminal where php main.php prints 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​

MistakeWhat happensDo this instead
Writing require 'greetings.php'; without __DIR__It depends on the folder you run PHP from, and can fail elsewhereStart the path with __DIR__
Loading the same file of functions twiceFatal 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.phpWrite __DIR__ . '/greetings.php'

Troubleshooting​

SymptomLikely causeFix
Warning: require(): Failed to open stream: No such file or directoryThe path is wrongCheck the file name and use __DIR__
Fatal error: Uncaught Error: Failed opening requiredThe same missing file, with require stopping the scriptFix 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​