How can externalizing connection data and including it in PHP files improve security and code organization?

Externalizing connection data and including it in PHP files can improve security by keeping sensitive information, such as database credentials, separate from the main codebase. This also enhances code organization by centralizing connection data in one location, making it easier to update and manage. Additionally, this approach allows for better control over access to the connection data, limiting exposure to potential security risks.

// config.php
<?php
$host = "localhost";
$username = "root";
$password = "password";
$database = "example_db";
?>

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

$conn = new mysqli($host, $username, $password, $database);

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