How can PHP be used to validate input from a form, such as ensuring that only letters are entered in a name field and only numbers in a zip code field?

To validate input from a form in PHP, you can use regular expressions to ensure that only specific types of characters are entered in each field. For example, to validate a name field to only allow letters, you can use a regular expression that matches only alphabetic characters. Similarly, for a zip code field, you can use a regular expression that matches only numeric characters.

// Validate name field to only allow letters
if (preg_match("/^[a-zA-Z ]+$/", $_POST['name'])) {
    // Name is valid
} else {
    // Name is not valid
}

// Validate zip code field to only allow numbers
if (preg_match("/^[0-9]+$/", $_POST['zip_code'])) {
    // Zip code is valid
} else {
    // Zip code is not valid
}