In PHP, what are the best practices for handling input validation to ensure that fields are not just filled with whitespace?

When handling input validation in PHP, it is important to ensure that fields are not just filled with whitespace. One way to achieve this is by using the trim() function to remove any leading or trailing whitespace from the input before validating it. This ensures that the input is not just empty spaces.

// Example of input validation with whitespace check
$input = $_POST['input_field'];

// Trim the input to remove leading and trailing whitespace
$input = trim($input);

// Check if the input is not empty after trimming
if (!empty($input)) {
    // Input is valid, proceed with further validation
    // ...
} else {
    // Input is empty or just whitespace
    echo "Input cannot be empty or just whitespace.";
}