How can one ensure consistent file path usage in PHP scripts for both web and command line execution to prevent errors when moving between environments?

To ensure consistent file path usage in PHP scripts for both web and command line execution, you can use the `__DIR__` magic constant to get the current directory of the script and then build paths relative to that. This way, the paths will be consistent regardless of the execution environment. Additionally, you can use the `DIRECTORY_SEPARATOR` constant to handle directory separators in a cross-platform manner.

// Define base path using __DIR__
$basePath = __DIR__;

// Define file paths relative to the base path
$filePath = $basePath . DIRECTORY_SEPARATOR . 'file.txt';

// Use the file path in your script
$fileContents = file_get_contents($filePath);
echo $fileContents;