How can a programmer differentiate between a server error and a script error in PHP?

To differentiate between a server error and a script error in PHP, a programmer can check the HTTP response code returned by the server. If the response code is in the 5xx range, it indicates a server error, while a 4xx response code typically indicates a script error.

// Check if the HTTP response code is in the 5xx range for a server error
if (http_response_code() >= 500 && http_response_code() < 600) {
    echo "Server error occurred.";
} elseif (http_response_code() >= 400 && http_response_code() < 500) {
    echo "Script error occurred.";
} else {
    echo "Unknown error occurred.";
}