How can error reporting be improved when encountering a "Could not connect to host" issue in PHP?

When encountering a "Could not connect to host" issue in PHP, it typically means there is a problem with the connection to the specified host, such as incorrect host address, port, or network issues. To improve error reporting in this situation, you can use the try-catch block to catch any exceptions thrown during the connection attempt and display a more informative error message to the user.

try {
    $connection = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}