What are the best practices for using HTML input values as PHP variables?

When using HTML input values as PHP variables, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One common practice is to use PHP's filter_input function to retrieve and sanitize the input values from the form fields.

// Retrieve and sanitize input values from form fields
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

// Use the sanitized input values in your PHP code
echo "Username: " . $username;
echo "Email: " . $email;