Is it best practice to store database connection details in a separate PHP file for security reasons?

It is considered best practice to store database connection details in a separate PHP file for security reasons. By keeping sensitive information like database credentials in a separate file, you can easily manage access permissions and prevent unauthorized access to the information. This also allows you to easily update the credentials in one central location without having to search through your codebase.

// db_config.php

define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');

// db_connection.php

require_once 'db_config.php';

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

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