Are there any best practices for error handling when validating text input in PHP?

When validating text input in PHP, it is important to handle errors gracefully to provide a better user experience. One best practice is to check for empty input and display a clear error message to the user if the input is invalid. Additionally, using functions like trim() to remove leading and trailing whitespace can help ensure the input is clean.

// Validate text input
$input = $_POST['text_input'];

// Check for empty input
if(empty($input)) {
    echo "Error: Text input cannot be empty.";
} else {
    // Remove leading and trailing whitespace
    $clean_input = trim($input);
    
    // Process the clean input
    // ...
}