What are the benefits of separating database connection logic into a separate PHP file for reusability?

Separating database connection logic into a separate PHP file allows for better organization and reusability of code. This approach makes it easier to manage database connections in one central location, reducing redundancy and making it simpler to update connection details if needed. Additionally, it promotes cleaner code structure and enhances code readability.

// db_connection.php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

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

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