How can the is_readable function in PHP help prevent errors when trying to read a file?

The is_readable function in PHP can help prevent errors when trying to read a file by checking if the file exists and is readable before attempting to open it. This can help avoid errors such as file not found or permission denied. By using is_readable, you can ensure that the file can be read before performing any file operations.

$file = 'example.txt';

if (is_readable($file)) {
    $content = file_get_contents($file);
    echo $content;
} else {
    echo "File is not readable or does not exist.";
}