How can PHP developers effectively handle user input in forms for calculations like BMI?

PHP developers can effectively handle user input in forms for calculations like BMI by validating and sanitizing the input data to ensure it is safe and accurate. They can then perform the necessary calculations using the validated input to calculate the BMI result. Finally, the BMI result can be displayed to the user in a clear and user-friendly manner.

<?php
// Validate and sanitize user input
$weight = filter_input(INPUT_POST, 'weight', FILTER_VALIDATE_FLOAT);
$height = filter_input(INPUT_POST, 'height', FILTER_VALIDATE_FLOAT);

// Check if input is valid
if ($weight && $height) {
    // Perform BMI calculation
    $bmi = $weight / ($height * $height);
    
    // Display BMI result
    echo "Your BMI is: " . number_format($bmi, 2);
} else {
    // Display error message if input is invalid
    echo "Invalid input. Please enter valid weight and height.";
}
?>