Skip to content
PHP Logo

How can relative path references be used effectively in PHP scripts to access a centralized config.php file located in a different directory?

When using relative path references in PHP scripts to access a centralized config.php file located in a different directory, it's important to understand the directory structure and use appropriate path traversal techniques. One effective way to do this is by using the `__DIR__` magic constant to get the current directory of the script and then navigating to the desired directory where the config.php file is located.

php
// Assume the config.php file is located in a directory named 'config' which is one level above the current script
require_once __DIR__ . '/../config/config.php';