0

I'm creating a BMI calculator with PHP and have run into some difficulties while testing. When I get a BMI equal to 18.5 it says it's underweight when it's supposed to say normal weight. I've tried many things to get the correct result but nothing works. The inputs I'm trying are Feet: 6, Inches: 5, and Pounds: 156.

$feet = $_GET['feet'];
$inches = $_GET['inches'];

$weight = $_GET['pounds'];
$height = (float) $feet * 12 + (float) $inches;

$bmi = ($weight/($height * $height)) * 703;

if ($bmi < 18.5)
    $result = "Underweight";
else if ($bmi >= 18.5 && $bmi <= 24.9)
    $result = "Normal weight";
else if ($bmi > 24.9 && $bmi <= 29.9)
    $result = "Overweight";
else
    $result = "Obese";

    printf("Your Height: %u' %u\"<br>Your Weight: %u lb<br>Your BMI is %.1f you are %s",$feet,$inches,$weight,$bmi,$result);

I've tried changing the conditionals for an hour and nothing has changed. What am I doing wrong?

  • Try adding a `var_dump($bmi);` after you make the calculation to confirm the value is what you are testing for. My guess is you are getting a value that is not exactly `18.5`. – Tigger Apr 30 '23 at 06:39
  • Can you `echo $bmi` and give us the result? I imagine it has something to do with the input. – GROVER. Apr 30 '23 at 07:02
  • You don't provide input data, but it's very unlikely that your calculation exactly equals 18.5 as IEEE 754 floating point. https://www.php.net/manual/en/language.types.float.php. BMI is not rocket science, I would try just rounding. – Álvaro González Apr 30 '23 at 09:20

1 Answers1

-1

You cannot directly compare the float values in PHP, instead you could use alternate methods mentioned in the referenced page :

Comparing float values in PHP

NIVID K
  • 9
  • 2