What are the potential pitfalls of trying to write PHP code to a file for configuration purposes?

Potential pitfalls of writing PHP code to a file for configuration purposes include security risks, readability issues, and maintenance challenges. To mitigate these risks, it is recommended to separate configuration data from executable code by using a different file format, such as JSON or XML, and ensuring that sensitive information is not exposed in the configuration file.

// Example of writing configuration data to a JSON file instead of PHP code
$configData = [
    'database' => [
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'password123',
        'dbname' => 'my_database'
    ],
    'debug' => true
];

$configFile = 'config.json';
file_put_contents($configFile, json_encode($configData));