I am going to use mysqli_close($connection) to close the $connection. But Before Closing I need to ensure that the concerned connection is open.
While some suggest to check for $connection->ping(),$connection->stat() or mysqli_thread_id($connection), the three will throw: 'Couldn't fetch mysqli' if the connection was closed before.
The solution that worked for me was:
if(is_resource($connection) && get_resource_type($connection)==='mysql link'){
$connection->close(); //Object oriented style
//mysqli_close($connection); //Procedural style
}
PS: Checking only if it's a resource could be enough in a controlled context.