How can PHP developers securely store and access MySQL connection details in a configuration file?

To securely store and access MySQL connection details in a configuration file, developers should use environment variables or a separate configuration file outside of the web root directory. This helps prevent sensitive information from being exposed in case of a security breach. By using these methods, developers can easily update connection details without changing the code.

// config.php

define('DB_HOST', getenv('DB_HOST'));
define('DB_USER', getenv('DB_USER'));
define('DB_PASS', getenv('DB_PASS'));
define('DB_NAME', getenv('DB_NAME'));

// db.php

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}