What are the best practices for sanitizing user input in PHP to prevent certain characters from being stored in variables?

To sanitize user input in PHP and prevent certain characters from being stored in variables, you can use the `filter_var` function with the `FILTER_SANITIZE_STRING` filter along with a custom list of disallowed characters. This will ensure that only safe characters are stored in the variable.

$userInput = $_POST['user_input']; // Assuming user input is coming from a POST request

// Define a list of disallowed characters
$disallowedChars = ['<', '>', '&', '"', "'", '/'];

// Sanitize user input and remove disallowed characters
$sanitizedInput = filter_var($userInput, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
$sanitizedInput = str_replace($disallowedChars, '', $sanitizedInput);

// Now $sanitizedInput contains the sanitized user input without disallowed characters