What are the best practices for handling mathematical operations with GET parameters in PHP?

When handling mathematical operations with GET parameters in PHP, it is important to validate and sanitize the input to prevent security vulnerabilities such as SQL injection or code injection. One way to do this is by using PHP's built-in functions like filter_input() or intval() to ensure that the input is a valid number before performing any calculations. Additionally, it is recommended to use type casting to explicitly convert the input to the desired data type to avoid unexpected results.

// Validate and sanitize the GET parameter
$number = filter_input(INPUT_GET, 'number', FILTER_VALIDATE_INT);

if ($number !== false) {
    // Perform mathematical operation
    $result = $number * 2;
    
    // Output the result
    echo "The result is: " . $result;
} else {
    // Handle invalid input
    echo "Invalid input. Please provide a valid number.";
}