Hi everybody,

I just started learning PHP and was asked to write a BMI calculator. I did all that but now I'm stuck with an if ... elseif that doesn't seem to want to work. I'm sure it's just a syntax error but could somebody look over it and see what I've done wrong with my if ... elseif? Remember, I'm new to this so my code isn't optimized or clean. Thanks in advance if you can help me out!

<?php
	$fname = $_POST['fname'];
	$weight = $_POST['weight'];
	$feet = $_POST['feet'];
	$inch = $_POST['inch'];

        $inches = ($feet * 12) + $inch;
	$total = $weight * 703 / ($inches * $inches);
	
	echo "First Name: ".$fname;
	echo "<br>";
	echo "Weight (lb): ".$weight;
	echo "<br>";
	echo "Height (ft/in): ".$feet;
	echo " feet ".$inch;
	echo " inches";
	echo "<br>";
	echo "<br>";
	echo "Your BMI is ".number_format($total, 2);
	echo "<br>";
	echo "<br>";
	if ($total <= 18.5)
		echo "You are underweight";
	else if (($total >= 18.6) && ($total <= 24.9))
		echo "You are at normal weight";
	else if (($total >= 25) && ($total <= 29.9))
		echo "You are overweight";
	else if ($total >= 30)
		echo "You are obese";
?>
Member Avatar for diafol
f ($total <= 18.5)
		echo "You are underweight";
	else if (($total >= 18.6) && ($total <= 24.9))
		echo "You are at normal weight";
	else if (($total >= 25) && ($total <= 29.9))
		echo "You are overweight";
	else if ($total >= 30)
		echo "You are obese";

You haven't used braces. This is the correct way to format ifs:

if(...){
  ...do this...
}elseif(...){
  ...do this...
}elseif(...){
  ...do this...
}else{
  ...do this...
}

Yay! Thank you so much. I didn't think to use braces :( and now feel kind of silly for not using them in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.