How can different types of errors, such as SQL errors or duplicate entries, be effectively caught and handled in PHP?
To effectively catch and handle different types of errors in PHP, you can use try-catch blocks to catch exceptions thrown by the code. For SQL errors, you can use the PDOException class to catch any database-related errors. For duplicate entries, you can check for them before inserting into the database using unique constraints or by querying the database to see if the entry already exists.
try {
// Code that may throw an exception, such as a SQL query
// Example:
$stmt = $pdo->prepare("INSERT INTO table_name (column_name) VALUES (:value)");
$stmt->execute(array(':value' => $input));
} catch (PDOException $e) {
// Handle SQL errors
echo "SQL Error: " . $e->getMessage();
} catch (Exception $e) {
// Handle other types of errors
echo "Error: " . $e->getMessage();
}