How can PHP be used to validate user input from a form before processing it?

When processing user input from a form in PHP, it is important to validate the data to ensure it meets the required criteria before processing it further. This helps prevent security vulnerabilities and ensures data integrity. One way to validate user input is by using PHP functions like `filter_var()` or regular expressions to check for specific patterns or formats.

// Example of validating user input from a form in PHP
$user_input = $_POST['user_input'];

// Validate if the input is not empty
if (!empty($user_input)) {
    // Validate if the input is a valid email address
    if (filter_var($user_input, FILTER_VALIDATE_EMAIL)) {
        // Process the input further
        echo "Input is valid: " . $user_input;
    } else {
        echo "Invalid email address";
    }
} else {
    echo "Input cannot be empty";
}