What are the best practices for storing database connection details in PHP scripts to avoid manual changes in multiple files?

Storing database connection details directly in PHP scripts can lead to manual changes in multiple files if the connection details need to be updated. To avoid this, a common practice is to store the connection details in a separate configuration file and include it in the PHP scripts that require database connectivity. This way, if the connection details change, you only need to update the configuration file.

// config.php
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "mydatabase";
?>

// index.php
<?php
include 'config.php';

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

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