What are the advantages of using a configuration file to store and load configuration data in PHP applications?

Using a configuration file to store and load configuration data in PHP applications allows for easier management and organization of settings. It also provides a centralized location for all configuration options, making it simpler to update and maintain. Additionally, separating configuration data from the main codebase enhances security by preventing sensitive information from being hard-coded directly into the application.

// config.php
return [
    'database' => [
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'password',
        'dbname' => 'my_database'
    ],
    'api_key' => 'my_api_key'
];

// index.php
$config = require 'config.php';

echo $config['database']['host']; // Output: localhost
echo $config['api_key']; // Output: my_api_key