How can PDO objects be utilized in PHP to establish connections with multiple databases more efficiently?

Using PDO objects in PHP allows for efficient connection management with multiple databases by creating separate instances of the PDO object for each database connection. This way, each database connection is isolated and can be managed independently. By utilizing PDO's features such as prepared statements and error handling, the code becomes more secure and maintainable.

// Establish connection to the first database
$dsn1 = 'mysql:host=localhost;dbname=database1';
$username1 = 'username';
$password1 = 'password';
$pdo1 = new PDO($dsn1, $username1, $password1);

// Establish connection to the second database
$dsn2 = 'mysql:host=localhost;dbname=database2';
$username2 = 'username';
$password2 = 'password';
$pdo2 = new PDO($dsn2, $username2, $password2);

// Use $pdo1 and $pdo2 for database operations