What security measures should be taken to protect sensitive information, such as database passwords, in PHP scripts?

Sensitive information, such as database passwords, should never be hard-coded directly into PHP scripts as it poses a security risk. Instead, these sensitive details should be stored in a separate configuration file outside of the web root directory and accessed securely using PHP constants or environment variables.

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

// database.php
require_once('config.php');

$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}