How can try-catch blocks be effectively used to handle exceptions in PHP?
Try-catch blocks can be effectively used in PHP to handle exceptions by enclosing the code that may throw an exception in a try block, and then catching and handling the exception in a catch block. This allows for graceful error handling and prevents the script from crashing when an exception occurs.
try {
// Code that may throw an exception
$result = 10 / 0;
} catch (Exception $e) {
// Handle the exception
echo "An error occurred: " . $e->getMessage();
}