How can the PHP function file_exists() be used effectively to check for file existence?

The PHP function file_exists() can be used effectively to check for the existence of a file before performing any operations on it, such as reading or writing. This can help prevent errors or issues that may arise from attempting to access a file that does not exist.

$filename = "example.txt";

if (file_exists($filename)) {
    echo "The file $filename exists.";
} else {
    echo "The file $filename does not exist.";
}