What are the benefits of using external configuration files for storing database connection details in PHP?
Storing database connection details directly in PHP files can pose security risks, as sensitive information like usernames and passwords are exposed. Using external configuration files to store these details provides better security by keeping them separate from the main codebase. It also allows for easier maintenance and flexibility, as connection details can be easily updated without modifying the code.
<?php
// config.php
return [
'host' => 'localhost',
'username' => 'root',
'password' => 'password',
'database' => 'dbname'
];
```
```php
<?php
// index.php
$config = include 'config.php';
$host = $config['host'];
$username = $config['username'];
$password = $config['password'];
$database = $config['database'];
// Connect to database using $host, $username, $password, and $database