What are the best practices for handling file opening errors in PHP to prevent error messages?

When handling file opening errors in PHP, it's important to check if the file exists and is readable before attempting to open it. To prevent error messages, you can use functions like `file_exists()` and `is_readable()` to validate the file before opening it. Additionally, using try-catch blocks can help catch any exceptions that may occur during file opening.

$file = 'example.txt';

if (file_exists($file) && is_readable($file)) {
    try {
        $handle = fopen($file, 'r');
        // File opening successful, proceed with reading or writing operations
    } catch (Exception $e) {
        echo 'Error opening file: ' . $e->getMessage();
    }
} else {
    echo 'File does not exist or is not readable';
}