What are the best practices for securely storing database connection details in PHP applications?

Storing database connection details securely in PHP applications is crucial to prevent unauthorized access to sensitive information. One best practice is to store these details in a separate configuration file outside the web root directory. This file should be restricted in terms of access permissions and should not be directly accessible via the web.

<?php
// config.php

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