When handling database connections in PHP, is it better to use die() or exit() for error handling, and why?
When handling database connections in PHP, it is better to use die() for error handling. Die() is a language construct that immediately terminates the script when an error occurs, providing a clear indication of the issue. Exit() can be used as well, but die() is more commonly used for error handling in PHP.
// Establish database connection
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}