Something I have always wondered, it seems that virtually no PHP applications ever explicitly close MySQL connections mysqli_close()
.
How does this work? Does PHP automagically close MySQL connections when scripts stop executing?
Something I have always wondered, it seems that virtually no PHP applications ever explicitly close MySQL connections mysqli_close()
.
How does this work? Does PHP automagically close MySQL connections when scripts stop executing?
Yes, the connection will be closed unless it is a persistent connection (opened with a p:
before the host). Non-persistent resources are freed automatically at the end of the script, because there are no more references to them.
Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.