Is it necessary to manually close the database connection in PHP, or does PHP automatically close it when the script ends?

It is good practice to manually close the database connection in PHP to ensure that resources are properly released and to prevent potential memory leaks. While PHP does automatically close the connection when the script ends, explicitly closing it can help improve performance and avoid potential issues in the future.

// Open a connection to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Perform database operations

// Close the database connection
mysqli_close($connection);