I’m fairly new and I need help with PHP, I’m doing a BMI calculator to calculate the BMI of a person by asking for weight and height and preffered system of measurements (metric/imperial), and assesing the user’s BMI (i.e BMI below normal, normal, and above normal), now my problem is that the output keeps giving me two assesment result like this: (please see the img attch’d. below.) ![see img here][1] I have no idea why is it giving me two “Your BMI is above normal” intead of just one. Please see the link to get a better understanding of what I’m trying to say: https://i.stack.imgur.com/EuMFP.jpg
here is my code:
<?php
$weight = $_POST["txtWeight"];
$height = $_POST["txtHeight"];
$unit = $_POST["optUnit"];
$metric = ($weight / ($height * $height));
$imperial = (($weight * 703)/($height * $height));
if ($unit == "metric")
echo "A height of $height Meters and a weight of $weight Kilograms = " .round($metric,1). " BMI.<br />";
{
if($metric <= 18.5)
echo " Your BMI is below normal";
else if ($metric >= 18.5 and $metric <= 24.9)
echo " Your BMI is normal";
else if($metric >= 25)
echo " Your BMI is above normal";
};
if ($unit == "imperial")
echo "A height of $height Inches and a weight of $weight Pounds = " .round($imperial,1). " BMI.<br />";
{
if( $imperial <= 18.5)
echo "Your BMI is below normal";
else if ($imperial >= 18.5 and $imperial <= 24.9)
echo "Your BMI is normal";
else if($imperial >= 25)
echo "Your BMI is above normal";
};
?>
My guess would be that your brackets are misplaced. Try this:
A better approach would be to condense your duplicate code: