What are the best practices for error handling in PHP when using functions like fopen?

When using functions like fopen in PHP, it is important to handle errors properly to ensure robustness in your code. One common practice is to check the return value of the function for errors and handle them accordingly by using conditional statements or try-catch blocks. Additionally, it is recommended to use error handling functions like error_reporting() and set_error_handler() to customize error handling behavior.

$filename = 'example.txt';

$file = fopen($filename, 'r');
if (!$file) {
    die("Error opening file");
}

// continue processing the file
fclose($file);