Hello all,

I have to write a program that calculate a bmi of person whose weight is 170 pounds (it increases by 1 pound for each month for 12 months) and 70 inches, after I have all the bmi for each weight from 170 - 182 pounds, under each weight I need to indicate if his bmi is underweight, normal, overweight or obese. I have written the program using php script, but it does not seem to work correctly, could anyone shed some light on this. The program code is:

<html>
<head>
<title>Lab 10a</title>
</head>
<body>
<?php
$pounds = 170;
$inches = 70;
$increase = 0;
$poundsinch = 703;
while ($increase < 13)
	{
	$division = ($pounds) / ($inches * $inches) * ($poundsinch);
	echo "The BMI of $pounds pounds and $inches inches is : ".$division."<br />";
	echo " ";
	$increase = $increase + 1;
	$pounds = $pounds + 1;
	if ($division < 18.5)
	  echo "You are underweight";
	elseif ($division < 25){
	  echo "Your BMI is normal";
	elseif ($division < 29.9)
	  echo "You are overweight";
	else{
	  echo "You are obese";
	
	}

	
	  
?>

</body>
</html>

Thanks in advance,
Tony

I assume this is homework.


Part of learning to program is to put a bit more thought into things.

Not so good: It's not working. It doesnt work right. It's broke.

Good: I was expecting to see X and I see Y. What is the command to ____.


When you start to think this way, and post this way, your errors will sometimes become apparent to you, and you won't need as much help.

Oh great. Now I have to google BMI and wade through corporate references or risk watching my intelligence decline.

Got it... BMI

Awful lot of science for something that within 15% is irrelevant.

Spending two seconds and only reading up to the first line of interest, the calculation of $DIVISION, I would start with looking up operator precedence in PHP and probably adding some parens.

To provide a useful answer:

  • use [code] and [/code] tags around your code so it is displayed on the forum in a readable fashion
  • use the "Preview Post" button to review your post before submitting it
  • be sure your code is properly indented
  • always use {} pairs around code blocks (while loops, if/else statements, etc.) even if there is only one statement in the block
  • verify that the braces are properly paired.

Your braces are neither balanced nor paired.

This is simply a matter of missing curly braces. Your script works as expected once they are all in there. Following Fest3er's advice will get you far.

As was stated above, the clearer the question, ther clearer that answer. However, thought I'd help the lad/ladette out....

<?php
$pounds = 170;
$inches = 70;
$increase = 0;
$poundsinch = 703;
while ($increase < 13){
	$division = number_format(($pounds) / ($inches * $inches) * ($poundsinch),2);
	echo "The BMI of $pounds pounds and $inches inches is : ".$division." ";
	if ($division < 18.5){
		echo "You are underweight.<br />";
		}
	elseif ($division < 25){
		echo "Your BMI is normal.<br />";
		}
	elseif ($division < 29.9){
		echo "You are overweight.<br />";
		}
	else{
		echo "You are obese.<br />";
		}
	$increase++;
	$pounds++;
	}
?>
This article has been dead for over six months. Start a new discussion instead.