What are best practices for validating and processing user input from HTML forms in PHP, especially when dealing with mathematical calculations?

When dealing with user input from HTML forms in PHP, especially when performing mathematical calculations, it is crucial to validate the input to ensure it is in the expected format and range. One best practice is to use PHP functions like `filter_input()` or `filter_var()` to sanitize and validate the input data. Additionally, always validate and sanitize user input before using it in any mathematical calculations to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks.

// Validate and process user input for mathematical calculations
$input_number = filter_input(INPUT_POST, 'number', FILTER_VALIDATE_FLOAT);

if($input_number !== false){
    // Perform mathematical calculations using the sanitized input
    $result = $input_number * 2;
    
    // Output the result
    echo "The result of multiplying the input number by 2 is: " . $result;
} else {
    // Handle invalid input
    echo "Invalid input. Please enter a valid number.";
}