How can error handling and debugging be improved in the PHP script to identify and resolve issues more effectively, especially in the context of file deletion operations?

Issue: To improve error handling and debugging in PHP script for file deletion operations, you can use try-catch blocks to catch exceptions and display meaningful error messages. Additionally, you can use functions like file_exists() and is_writable() to check if the file exists and is writable before attempting to delete it.

<?php

$filename = 'example.txt';

try {
    if (!file_exists($filename)) {
        throw new Exception('File does not exist.');
    }

    if (!is_writable($filename)) {
        throw new Exception('File is not writable.');
    }

    if (!unlink($filename)) {
        throw new Exception('Error deleting file.');
    }

    echo 'File deleted successfully.';
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

?>