What is the best practice for passing variables via POST in PHP forms?

When passing variables via POST in PHP forms, it is best practice to sanitize and validate the input data to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to achieve this is by using the filter_input() function to access and filter input variables. This function allows you to specify the type of input you are expecting and apply filters to sanitize the data before using it in your application.

// Sanitize and validate input data passed via POST
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

// Check if the input data is valid before using it
if ($username && $email) {
    // Process the input data
    echo "Username: " . $username . "<br>";
    echo "Email: " . $email;
} else {
    echo "Invalid input data";
}