What best practices should be followed when handling user input for mathematical operations in PHP?

When handling user input for mathematical operations in PHP, it is important to validate and sanitize the input to prevent security vulnerabilities such as SQL injection or code execution. One way to achieve this is by using PHP's filter_input function with FILTER_VALIDATE_FLOAT or FILTER_VALIDATE_INT filters to ensure that the input is a valid number.

$input = filter_input(INPUT_POST, 'number', FILTER_VALIDATE_FLOAT);

if ($input === false) {
    // Handle invalid input error
} else {
    // Perform mathematical operations using the sanitized input
    $result = $input * 2;
    echo "Result: " . $result;
}