What are the best practices for handling user input and validating correct answers in a PHP quiz application?

When handling user input and validating correct answers in a PHP quiz application, it is important to sanitize and validate the input to prevent security vulnerabilities and ensure accurate results. One way to do this is by using PHP's filter_input function to retrieve and validate user input from the quiz form.

// Retrieve user input from the quiz form
$user_answer = filter_input(INPUT_POST, 'answer', FILTER_SANITIZE_STRING);

// Validate the user answer against the correct answer
$correct_answer = "correct_answer_here";

if ($user_answer === $correct_answer) {
    // User provided the correct answer
    echo "Congratulations! Your answer is correct.";
} else {
    // User provided the wrong answer
    echo "Sorry, your answer is incorrect. Please try again.";
}