How can the use of external configuration files improve the organization and security of PHP scripts?

Using external configuration files can improve the organization and security of PHP scripts by separating sensitive information such as database credentials or API keys from the main script. This helps in keeping the code clean and easy to manage, as well as reducing the risk of exposing sensitive data in case of a security breach.

// config.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');
?>

// main script
<?php
require_once 'config.php';

$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// rest of the script
?>