0

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";
?>
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by smush
0
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...
}
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.