What is the purpose of using a config file in PHP and how can it be effectively integrated into a project?
Using a config file in PHP allows for the centralization of configuration settings such as database credentials, API keys, and other constants used throughout a project. This helps to maintain a clean and organized codebase, as well as make it easier to update configuration settings without having to search through multiple files.
// config.php
return [
'db_host' => 'localhost',
'db_name' => 'my_database',
'db_user' => 'root',
'db_pass' => 'password123'
];
// index.php
$config = require 'config.php';
$db = new PDO("mysql:host={$config['db_host']};dbname={$config['db_name']}", $config['db_user'], $config['db_pass']);