What function or method can be used to validate input in a PHP form?

Validating input in a PHP form is crucial to ensure that the data submitted by users is in the correct format and meets certain criteria. One common method to validate input is by using PHP's filter_var() function, which can validate and sanitize different types of data such as email addresses, URLs, integers, and more. By using filter_var() with the appropriate filter flags, you can easily validate user input and prevent malicious data from being submitted.

// Example of validating an email address input in a PHP form
$email = $_POST['email'];

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Email address is valid, proceed with processing the form data
} else {
    // Email address is not valid, display an error message to the user
    echo "Invalid email address";
}