What are the best practices for handling errors and warnings when using get_headers in PHP?

When using the get_headers function in PHP, it is important to handle errors and warnings that may occur, such as network timeouts or invalid URLs. One way to handle these errors is by using error handling functions like try-catch blocks to catch exceptions and display appropriate error messages to the user.

$url = 'http://example.com';

try {
    $headers = get_headers($url);
    
    if ($headers === false) {
        throw new Exception('Error retrieving headers for ' . $url);
    }
    
    // Process headers here
} catch (Exception $e) {
    echo 'An error occurred: ' . $e->getMessage();
}