Are there any best practices for handling errors in PHP code, especially within if statements?

When handling errors in PHP code, especially within if statements, it is important to check for errors and handle them appropriately to prevent unexpected behavior or crashes. One best practice is to use try-catch blocks to catch exceptions and handle errors gracefully.

try {
    // Code that may throw an exception
    if ($condition) {
        throw new Exception('Error message');
    }
} catch (Exception $e) {
    // Handle the error gracefully
    echo 'An error occurred: ' . $e->getMessage();
}