How can constants be effectively used to manage database connections and configurations within PHP functions?

Constants can be effectively used to manage database connections and configurations within PHP functions by defining them at the beginning of the script and then referencing them throughout the code. This allows for easy modification of connection details in one central location, making it simpler to update credentials or switch between different databases.

define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');

function connectToDatabase(){
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    return $conn;
}