How can error handling be improved in the given PHP code snippet to prevent unexpected behavior?

The issue in the given PHP code snippet is that error handling is not properly implemented, which can lead to unexpected behavior. To improve error handling, we can use try-catch blocks to catch exceptions and handle errors gracefully. By doing so, we can prevent the script from crashing and provide informative error messages to the user.

try {
    // Code that may throw exceptions or errors
    $result = someFunction();
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage();
} catch (Error $e) {
    echo "A fatal error occurred: " . $e->getMessage();
}

function someFunction() {
    // Function implementation
}