Compare the Return Value of PHP include Correctly

Last updated: December 1, 2026.

Assign the result of include to a variable before comparing it. Parentheses around the filename alone can still leave the comparison inside the path expression.

<?php
// feature-mode.php, stored beside the caller:
// <?php return 'enabled';

$mode = include __DIR__ . '/feature-mode.php';
if ($mode === false) {
    throw new RuntimeException('Could not load feature mode');
}

if ($mode === 'enabled') {
    echo "Feature enabled", PHP_EOL;
}

Group the full include expression

include is a language construct. Even if an error trace prints it in function-like form, writing parentheses around the filename does not group the full include expression. If you need a one-line condition, use if ((include $path) === 'enabled'). With a separate assignment, the filename and comparison are easier to inspect.

Use a fixed path rooted at __DIR__ when the file belongs to the application. Avoid accepting an include filename directly from a request. If the application offers several selectable configuration files, map a known key to a fixed path.

Distinguish failure from a valid returned value

A successful include normally evaluates to integer 1 if the file does not explicitly return a value. A return inside the included file ends that file and supplies the result to the caller. A load failure emits a warning and returns false. If the file intentionally returns false too, a false value alone cannot distinguish those cases. In the example, the file contract expects a mode string.

Use require when the application cannot continue without the file and should treat a load failure as an error. Test with an intended return value and a fixture with no explicit return to make sure the caller handles both as designed.

Documentation

Related guides

Sergey Kornilov

Sergey Kornilov