How can you differentiate the response in a destructor based on whether the script ended normally or an error occurred in PHP?

To differentiate the response in a destructor based on whether the script ended normally or an error occurred in PHP, you can use the `error_get_last()` function to check if an error occurred during script execution. If an error is present, you can handle it accordingly in the destructor. This allows you to perform different actions based on whether the script ended normally or an error occurred.

class MyClass {
    public function __destruct() {
        $error = error_get_last();
        
        if ($error !== null) {
            // Handle error in destructor
            echo "An error occurred: " . $error['message'];
        } else {
            // Perform actions for normal script termination
            echo "Script ended normally";
        }
    }
}

// Create an instance of MyClass
$obj = new MyClass();

// Trigger an error
trigger_error("An error occurred", E_USER_ERROR);