How does PHP script know where to look for the database on localhost when connecting?

When connecting to a database on localhost using PHP, the script needs to specify the host as "localhost" and provide the database name, username, and password. This information is used by PHP to establish a connection to the MySQL database running on the localhost server.

<?php
$host = "localhost";
$dbname = "database_name";
$username = "username";
$password = "password";

$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
?>