What are the best practices for error handling and debugging in PHP to ensure smooth functioning of web applications?

To ensure smooth functioning of web applications, it is important to implement proper error handling and debugging practices in PHP. This includes using try-catch blocks to handle exceptions, logging errors to a file or database, and displaying user-friendly error messages. Additionally, using debugging tools like Xdebug can help in identifying and fixing issues in the code.

try {
    // code that may throw an exception
} catch (Exception $e) {
    // log the error
    error_log($e->getMessage(), 3, "error.log");

    // display a user-friendly error message
    echo "An error occurred. Please try again later.";
}