What are the best practices for storing sensitive data, such as MySQL credentials, in PHP scripts?

Storing sensitive data, such as MySQL credentials, in PHP scripts can pose a security risk if not handled properly. One best practice is to store the credentials in a separate configuration file outside the web root directory, and then include this file in your PHP script. This helps prevent the credentials from being exposed to unauthorized users.

<?php
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database');

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

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