Are PHP include paths relative to the file or the calling code?

Are PHP include paths relative to the file or the calling code?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Are PHP include paths relative to the file or the calling code?

When you execute a PHP script, the engine resolves relative paths in include and require statements against a specific anchor point. The core rule is straightforward: the path is always relative to the directory containing the file that contains the statement, not the file currently being parsed or the one that called it.

If File A includes File B, and File B subsequently includes File C using a relative path, the resolution happens at the moment the interpreter reads the include directive inside File B. At that exact instant, PHP treats the location of File B as the current working directory for the purpose of that specific statement. It does not matter where File A resides or which script originally triggered the execution; only the physical location of the file defining the include path matters.

To illustrate this behavior concretely, consider a project structure like this:

/project-root
    /controllers
        index.php
    /includes
        helpers.php
        config.php

If index.php contains require 'includes/config.php', it looks for the file in the directory of index.php. If config.php then attempts to include another utility file with include '../helpers/functions.php', PHP calculates that path relative to config.php's location, not index.php's. The chain of execution follows the stack of loaded files, resolving each path independently based on its own file context.

This behavior often leads to confusion because developers frequently equate "current working directory" with the directory of the originally executed script. In many command-line contexts or web server environments, the working directory can indeed be different from the script location. For instance, a CLI script might run from /var/www but reference files in /home/user/projects. However, relative includes inside that script are still resolved against the script's own directory, not the OS-level current working directory unless you explicitly manipulate it.

You can verify the effective base path used for resolution by checking the __FILE__ and dirname(__FILE__) constants. These provide the absolute path of the currently executing file within the PHP process. By echoing these inside your included files, you can debug exactly where the interpreter thinks it is when resolving paths.

// Inside helpers.php
echo "File is located at: " . __FILE__;
echo "Directory is: " . dirname(__FILE__);

Understanding this distinction is critical for portability. If your application structure shifts or if you copy a directory into a different server environment without adjusting the relative paths, hard-coded assumptions about the entry point can break. For example, relying on include './views/layout.php' inside a controller assumes that all controllers sit in a specific subdirectory relative to a central views folder. If you move a controller up a level or create an alias, that include will fail unless the path is recalculated based on the new file location.

One common pitfall arises when mixing require statements with manual path manipulation functions like getcwd(). While getcwd() returns the current working directory of the CLI process or web server (often / or the document root), PHP does not use this value to resolve relative includes by default. It uses the directory of the file containing the statement. Relying on getcwd() for path construction inside includes is generally an anti-pattern because it couples your logic to the environment where the script was launched rather than the logical structure of the codebase.

If you are managing a complex framework or building reusable libraries, this behavior dictates how you organize your autoloading strategies. Modern solutions like Composer’s PSR-4 autoloaders bypass manual includes entirely by mapping namespaces to directories, effectively solving the "relative vs absolute" problem by using absolute paths derived from the vendor directory regardless of which file triggers the load. If you are working in the Laravel ecosystem, similar concerns about path resolution apply, though Laravel provides a robust service container and filesystem helpers that abstract away much of this manual path juggling. For more on managing dependencies and paths efficiently in modern PHP applications, you might explore resources like https://laravelcompany.com.

The takeaway is to always define your include paths relative to the file they reside in, or better yet, switch to absolute paths calculated at runtime using __DIR__. This ensures your code remains resilient to changes in entry points and avoids subtle bugs caused by assumptions about where the script started versus where the logic currently sits.

Our network