How can passing database connections as parameters improve code efficiency in PHP?

Passing database connections as parameters can improve code efficiency in PHP by promoting reusability and reducing the need to create multiple connections within the same script. By passing the connection as a parameter to functions or methods, you can ensure that the same connection is used throughout the script, avoiding unnecessary overhead from establishing new connections each time.

// Function that takes a database connection as a parameter
function fetchData($connection) {
    $query = "SELECT * FROM table";
    $result = mysqli_query($connection, $query);
    
    // Process the result
    // ...
}

// Establish a database connection
$connection = mysqli_connect("localhost", "username", "password", "database");

// Call the function with the database connection
fetchData($connection);