What are the potential performance implications of creating a new connection vs. reopening an existing one in PHP?

Creating a new connection every time a script runs can lead to performance issues due to the overhead of establishing a connection. Reusing an existing connection can improve performance by reducing the time it takes to establish a connection.

// Create a function to establish a database connection if one doesn't already exist
function getDbConnection() {
    static $conn;

    if (!$conn) {
        $conn = new mysqli("localhost", "username", "password", "database");
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
    }

    return $conn;
}

// Example usage
$conn = getDbConnection();