How can one ensure proper error handling in a PHP script, especially when dealing with external services like GeoIP lookup?

Proper error handling in a PHP script when dealing with external services like GeoIP lookup involves using try-catch blocks to catch any exceptions that may occur during the service call. This ensures that the script can gracefully handle errors and prevent them from crashing the entire application.

try {
    // Code for GeoIP lookup service call
    // If an error occurs, throw an exception
} catch (Exception $e) {
    // Handle the error gracefully, log it, and/or display a user-friendly message
    echo "An error occurred: " . $e->getMessage();
}