How can you check if a file already exists before opening it in PHP?

When working with files in PHP, it is important to check if a file already exists before attempting to open it to prevent errors or overwriting existing data. This can be done using the `file_exists()` function in PHP, which returns true if the file exists and false if it does not. By checking if the file exists before opening it, you can ensure that your code behaves as expected and avoids any potential issues.

$filename = 'example.txt';

if (file_exists($filename)) {
    $file = fopen($filename, 'r');
    // Perform operations on the file
    fclose($file);
} else {
    echo "File does not exist.";
}