What are best practices for handling file paths in PHP when using functions like file_exists()?

When working with file paths in PHP, it's important to ensure that paths are properly formatted to avoid errors when using functions like file_exists(). To handle file paths correctly, it's recommended to use the DIRECTORY_SEPARATOR constant to separate directories in paths. This constant will automatically use the correct separator for the operating system, ensuring compatibility across different platforms.

// Example of using DIRECTORY_SEPARATOR for handling file paths
$filePath = 'path' . DIRECTORY_SEPARATOR . 'to' . DIRECTORY_SEPARATOR . 'file.txt';

if (file_exists($filePath)) {
    echo 'File exists!';
} else {
    echo 'File does not exist.';
}