Php end of script / closing database

I’m creating some classes in PHP, and I was wondering - at the end of a php script does the mysql database connection automatically close (it’s a non-persistent connection)?

is it more efficient or necessary to close the database?

Yes, all resources (file handles, database connections, sockets, etc) opened by the script will be cleaned up at the end of execution. You don’t need to explicitly close the connection unless you want to end it early, like in the middle of your code before running some long task that doesn’t use the database.

Will having the database open through an entire script slow the script down or slow the server down?
or will it simply just leave the connection open making one less connection for someone else to be able to use until the script finishes?

no the rest of the script won’t take longer than a second to run.

If you close it earlier, it will save one connection for that 0.01 seconds of time. At the end of the script, php will automatically close the connection. Some people say that if you close the connection early, it may save speed, but this will also cause errors if you need to use it after you closed the connection.

Personally, I do not close the connection because it closes it automatically.

It simply just leave the connection open making one less connection for someone else to be able to use until the script finishes.

If you are going to tie up a resource like that for a whole second if you don’t close the database then you should close it. If it’s only going to be a few thousand statements that are only going to take milliseconds to run then it probably isn’t worth it.