How can relative paths in PHP scripts be handled to ensure consistent behavior when run through different methods?

Relative paths in PHP scripts can be handled by using the `__DIR__` magic constant to get the absolute path of the current script directory. By concatenating this with the relative path, you can ensure consistent behavior regardless of how the script is run. This approach ensures that the script can locate files or directories relative to its own location.

// Get the absolute path of the current script directory
$basePath = __DIR__;

// Define the relative path to the file or directory
$relativePath = 'folder/file.txt';

// Concatenate the base path with the relative path
$fullPath = $basePath . '/' . $relativePath;

// Use $fullPath to access the file or directory
echo file_get_contents($fullPath);