What are the best practices for handling errors and exceptions in PHP scripts instead of using die()?

When handling errors and exceptions in PHP scripts, it is best practice to use try-catch blocks to catch exceptions and handle errors gracefully instead of abruptly terminating the script with die(). This allows for better error handling, logging, and displaying user-friendly error messages.

try {
    // code that may throw an exception or error
    $result = 1 / 0;
} catch (Exception $e) {
    // handle the exception
    echo "An error occurred: " . $e->getMessage();
} catch (Error $e) {
    // handle the error
    echo "A fatal error occurred: " . $e->getMessage();
}