Zum Hauptinhalt springen

Magic Constants

What does __DIR__ do?

include dirname(__DIR__) . '/config/db.php';

__DIR__ is a magic constant which returns the absolute filesystem path of the file where it is used. It does not depend on where the script is executed from; it’s based on the file’s location on disk.

If we call:

echo __DIR__; in the file above it will output (XAMPP / local machine):

C:\xampp\htdocs\project-name\config\

The function dirname() removes the last segment (file or folder) from the path and returns what's left — the directory one level up, e.g.:

echo dirname("/var/www/html/index.php");
Output: /var/www/html

__DIR__ 👉 where this file is

dirname(__DIR__) 👉 one level above this file