What are some best practices for processing form data in PHP, especially when dealing with predefined answers?

When processing form data in PHP, especially when dealing with predefined answers, it is important to sanitize and validate the input to prevent security vulnerabilities and ensure data integrity. One best practice is to use PHP's in_array() function to check if the submitted value is within a predefined list of acceptable answers.

// Define an array of predefined answers
$predefined_answers = array("Option 1", "Option 2", "Option 3");

// Check if the submitted value is within the predefined answers
if(in_array($_POST['answer'], $predefined_answers)) {
    // Process the form data
    $selected_answer = $_POST['answer'];
    // Additional processing logic here
} else {
    // Handle invalid input
    echo "Invalid answer selected";
}