What are some best practices for structuring PHP scripts to avoid path-related issues when running them through different methods?

Path-related issues in PHP scripts can arise when the script is run through different methods, such as through the command line or a web server. To avoid these issues, it is best to use absolute paths instead of relative paths when referencing files or directories within the script. This ensures that the script can locate the necessary resources regardless of how it is executed.

// Using absolute paths to avoid path-related issues
$filePath = '/var/www/html/myfile.txt';
$fileContents = file_get_contents($filePath);

// Alternatively, you can use the __DIR__ magic constant to get the current directory
$filePath = __DIR__ . '/myfile.txt';
$fileContents = file_get_contents($filePath);