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.
Just don't close it. That would be the best solution ever.
99.9% of time it's perfectly ok to leave the connection alone, PHP will close it for you.
Only if your script has to perform some heavy time consuming task that doesn't involve a database interaction, you may want to close the connection before starting this operation. But in this case you will have the connection deliberately open as there will be no random closures scattered around the code and therefore will be no need to check whether it is closed already. Hence, just close it, in this particular but extremely rare case.
All other time just leave it alone.