What are some best practices for sanitizing user input in PHP to ensure only numeric values are stored in a variable?
To sanitize user input in PHP to ensure only numeric values are stored in a variable, you can use the `filter_var()` function with the `FILTER_SANITIZE_NUMBER_INT` filter. This filter will remove all non-numeric characters from the input. Additionally, you can use the `is_numeric()` function to check if the input is a valid numeric value before storing it in a variable.
// Sanitize user input to ensure only numeric values are stored in a variable
$userInput = "123abc";
$numericValue = filter_var($userInput, FILTER_SANITIZE_NUMBER_INT);
// Check if the input is a valid numeric value
if (is_numeric($numericValue)) {
// Store the numeric value in a variable
$finalValue = $numericValue;
} else {
// Handle the case where the input is not a valid numeric value
echo "Invalid input. Please enter a numeric value.";
}