What are the best practices for error handling and debugging in PHP code?

When handling errors in PHP code, it is important to use try-catch blocks to catch exceptions and handle them gracefully. Additionally, using error_reporting() function to set the level of error reporting can help in debugging. Utilizing tools like Xdebug for debugging and logging errors can also be beneficial.

try {
    // code that may throw an exception
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage();
}

// Set error reporting level
error_reporting(E_ALL);

// Debugging with Xdebug
xdebug_start_trace('my_trace');
// code to debug
xdebug_stop_trace();