November 25, 2026 - 09:44
PHP Lesson 16 — include vs require: What Happens When a File Is Missing? Image
PHP Tutorials

PHP Lesson 16 — include vs require: What Happens When a File Is Missing?

0 Comments

When we split the task-list project into files, we wrote require __DIR__ . '/../app/functions.php';. Once it worked, it was easy to move on. But what happens if the filename is wrong? Why do other examples use include or require_once?

Create a separate folder called include-experiment. All the files below belong there, and you should open the terminal in that folder. No database or web server is needed. Keep this experiment separate from the project because we will deliberately refer to missing files.

Run one file from another

Create message.php first:

PHP
<?php
$title = 'Task list';
echo "Message loaded", PHP_EOL;

In the same folder create index.php:

PHP
<?php
include __DIR__ . '/message.php';

echo $title, PHP_EOL;
echo "After include", PHP_EOL;

Running php index.php produces three lines: Message loaded, Task list, and After include. The include did more than mention a filename: PHP executed the file at that point. We could then read the $title variable defined inside it.

Including a file does not make a separate browser request. Its code joins the current execution. HTML in that file becomes output; a variable defined there is available in the scope where the include happens. If you include a file inside a function, remember that the scope is different from this top-level example.

What does include do when the file is missing?

Create include-missing.php as follows, but deliberately do not create missing.php:

PHP
<?php
include __DIR__ . '/missing.php';

echo "After include", PHP_EOL;

Run php include-missing.php. You get warnings that the file could not be opened, followed by After include. With the standard PHP error handling used here, execution continues after the warnings. We have not installed a custom error handler that changes that behavior.

Now create require-missing.php:

PHP
<?php
require __DIR__ . '/missing.php';

echo "After require", PHP_EOL;

php require-missing.php produces the file-opening warning and then stops with an uncaught Error. You do not see After require. PHP 8 allows this error to be caught, but this example has no catch block, so execution ends here.

If the application cannot meaningfully continue without its connection settings or helper functions, require expresses that dependency. Choosing include does not make a missing file harmless: you still need to decide what continuing without it means.

Why is __DIR__ in the path?

__DIR__ gives the directory containing the file where that code appears. The dot concatenates it with '/message.php' to form the path. We explicitly choose the path's starting point instead of relying on an assumption about the working directory.

The .. in a path means the parent directory. From the course's public/index.php, ../app/functions.php goes up out of public and into app. Keep filename capitalization consistent with the actual file too.

Load the same function file twice

Put a small function in helpers.php:

PHP
<?php
function doubleNumber(int $number): int
{
    return $number * 2;
}

Create duplicate.php to load it twice:

PHP
<?php
require __DIR__ . '/helpers.php';
require __DIR__ . '/helpers.php';

echo doubleNumber(3), PHP_EOL;

php duplicate.php fails with Cannot redeclare. The second include tries to define the same function again, before we even reach the call.

In once.php, change just the loading mechanism:

PHP
<?php
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/helpers.php';

echo doubleNumber(3), PHP_EOL;

php once.php prints 6. require_once does not execute a file already included in this execution. include_once combines the same repeat check with include's failure behavior. “Once” does not mean once for the entire lifetime of the server; a new PHP execution may load the file again.

An included file can return a value

Create config.php:

PHP
<?php
return ['limit' => 10];

Then create read-config.php:

PHP
<?php
$config = require __DIR__ . '/config.php';

echo $config['limit'], PHP_EOL;

php read-config.php prints 10. That is the idea behind the course's $pdo = require ...: execute the file and collect its return value. Do not mechanically replace every such require with require_once. If the file is already loaded, another require_once does not execute it again to reproduce its return value.

Where this appeared in the task list: PHP Lesson 04 — Organize the Code with Functions and Files

Official documentation: include · require · require_once · __DIR__

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment