What are some ways to handle fatal errors in PHP, such as using functions like mysql_error() or try-catch blocks?

When handling fatal errors in PHP, it is important to use functions like mysql_error() to retrieve error messages from MySQL queries or try-catch blocks to catch exceptions and handle errors gracefully. By using these methods, you can provide more informative error messages to users and prevent your application from crashing unexpectedly.

// Example using mysql_error() to handle fatal MySQL errors
$query = "SELECT * FROM non_existent_table";
$result = mysql_query($query);

if (!$result) {
    die("Error: " . mysql_error());
}

// Example using try-catch block to handle fatal errors
try {
    $file = fopen("non_existent_file.txt", "r");
    if (!$file) {
        throw new Exception("Could not open file.");
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}