How can sensitive information like database credentials be securely managed and masked in PHP projects to prevent unauthorized access?

Sensitive information like database credentials can be securely managed and masked in PHP projects by storing them in environment variables and accessing them using PHP's getenv() function. This prevents unauthorized access to the credentials as they are not hard-coded in the codebase. Here is an example of how to implement this:

// Store database credentials in environment variables
putenv("DB_HOST=localhost");
putenv("DB_USER=username");
putenv("DB_PASS=password");
putenv("DB_NAME=database_name");

// Access database credentials using getenv()
$host = getenv("DB_HOST");
$user = getenv("DB_USER");
$pass = getenv("DB_PASS");
$dbname = getenv("DB_NAME");

// Connect to the database using the credentials
$conn = new mysqli($host, $user, $pass, $dbname);

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