What are some best practices for error handling when using fopen in PHP?

When using fopen in PHP, it is important to handle errors that may occur during file operations. One best practice is to check if the file was opened successfully before proceeding with any read or write operations. This can be done by checking the return value of fopen for false, indicating that the file failed to open.

$filename = "example.txt";
$handle = fopen($filename, "r");

if ($handle === false) {
    die("Error opening file");
}

// Proceed with file operations