Skip to content
PHP Logo

What are the potential pitfalls of including configuration settings directly in PHP files instead of using a centralized approach?

Including configuration settings directly in PHP files can lead to scattered and duplicated code, making it difficult to manage and update settings across multiple files. It can also pose a security risk if sensitive information, such as database credentials, is included in plain text within the PHP files. To solve this issue, it's recommended to use a centralized approach, such as storing configuration settings in a separate configuration file that can be easily accessed and updated.

php
// config.php
<?php
return [
    'db_host' => 'localhost',
    'db_name' => 'my_database',
    'db_user' => 'root',
    'db_pass' => 'password123'
];
```

```php
// index.php
<?php
$config = require 'config.php';

echo $config['db_host']; // Output: localhost
echo $config['db_name']; // Output: my_database
echo $config['db_user']; // Output: root
echo $config['db_pass']; // Output: password123