What are best practices for defining and using host, username, password, and database constants in PHP when connecting to a database?

When connecting to a database in PHP, it is best practice to define host, username, password, and database constants to securely store this sensitive information and easily manage any changes. By using constants, you can easily update these values in one place if needed without having to search through your code. This also helps improve security by preventing accidental exposure of credentials in your code.

define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'database_name');

$connection = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}