How can PHP handle the addition of numerical values from form inputs properly?

When handling numerical values from form inputs in PHP, it's important to ensure that the inputs are properly sanitized and validated to prevent any potential security risks or unexpected behavior. To handle addition of numerical values correctly, you should first check if the input values are numeric using functions like is_numeric() or is_int(), then sanitize the inputs using filter_var() or intval() to remove any non-numeric characters. Finally, perform the addition operation on the sanitized values.

// Sample PHP code snippet to handle addition of numerical values from form inputs
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];

// Validate inputs
if (is_numeric($input1) && is_numeric($input2)) {
    // Sanitize inputs
    $num1 = intval($input1);
    $num2 = intval($input2);

    // Perform addition
    $result = $num1 + $num2;

    echo "The sum of $num1 and $num2 is: $result";
} else {
    echo "Invalid input. Please enter numeric values.";
}