What potential pitfalls should be considered when passing a database connection variable in PHP functions?

Passing a database connection variable in PHP functions can lead to potential pitfalls such as making the code less readable and maintainable, as well as increasing the risk of accidentally overwriting the connection variable. To avoid these issues, it's recommended to use dependency injection to pass the database connection variable to functions when needed.

// Avoid passing database connection variable directly to functions
function getUserData($db) {
    $query = "SELECT * FROM users";
    $result = $db->query($query);
    // Process the result
}

// Implementing dependency injection to pass the database connection variable
$db = new mysqli('localhost', 'username', 'password', 'database');
getUserData($db);