What is Body Mass Index ?

Body mass index (BMI) is a measurement of body fat based on height and weight that applies to adult men and women.

Body Mass Index result into score that Classification into different ranges, if result under these value .

  • Less than 15 Very severely underweight
  • Between 15 and 16 Severely underweight
  • Between 16 and 18.5 Underweight
  • Between 18.5 and 25 Normal (healthy weight)
  • Between 25 and 30 Overweight
  • Between 30 and 35 Moderately obese
  • Between 35 and 40 Severely obese
  • Over 40 Very severely obese

Function

function bmi_calculate($mass, $height, $unit) {
		$bmi = 0;
		if ($unit == "Metric") {
			//BMI = weight (kg) ÷/ ( height * height  ) in Meter
			$bmi = ($mass / ($height * $height));
		}
		if ($unit == "Imperial") {
			//BMI = weight (lb) x (height inch  * height inch ) x 703
			$bmi = ($mass / ($height * $height)) * 703;
		}
		return round($bmi, 1);
	}

	function bmi_weight($bmi) {
		$output = "";
		/*
		 Less than 15	Very severely underweight
		 Between 15 and 16	Severely underweight
		 Between 16 and 18.5	Underweight
		 Between 18.5 and 25	Normal (healthy weight)
		 Between 25 and 30	Overweight
		 Between 30 and 35	Moderately obese
		 Between 35 and 40	Severely obese
		 Over 40	Very severely obese
		 */
		if ($bmi < 15) {
			$output = "Very severely underweight";
		} else if ($bmi >= 15 && $bmi < 16) {
			$output = "	Severely underweight";
		} else if ($bmi >= 16 && $bmi < 18.5) {
			$output = "	Underweight";
		} else if ($bmi >= 18.5 && $bmi < 25) {
			$output = "Normal (healthy weight)";
		} else if ($bmi >= 25 && $bmi < 30) {
			$output = "Overweight";
		} else if ($bmi >= 30 && $bmi < 35) {
			$output = "Moderately obese";
		} else if ($bmi >= 35 && $bmi < 40) {
			$output = "Severely obese";
		} else {
			$output = "Very severely obese";
		}
		return $output;
	}

Program

<?php
	function covert_feet_inch($feet) {
		return ($feet * 12);
	}
	function bmi_calculate($mass, $height, $unit) {
		$bmi = 0;
		if ($unit == "Metric") {
			//BMI = weight (kg) ÷/ ( height * height  ) in Meter
			$bmi = ($mass / ($height * $height));
		}
		if ($unit == "Imperial") {
			//BMI = weight (lb) x (height inch  * height inch ) x 703
			$bmi = ($mass / ($height * $height)) * 703;
		}
		return round($bmi, 1);
	}
	function bmi_weight($bmi) {
		$output = "";
		/*
		 Less than 15	Very severely underweight
		 Between 15 and 16	Severely underweight
		 Between 16 and 18.5	Underweight
		 Between 18.5 and 25	Normal (healthy weight)
		 Between 25 and 30	Overweight
		 Between 30 and 35	Moderately obese
		 Between 35 and 40	Severely obese
		 Over 40	Very severely obese
		 */
		if ($bmi < 15) {
			$output = "Very severely underweight";
		} else if ($bmi >= 15 && $bmi < 16) {
			$output = "	Severely underweight";
		} else if ($bmi >= 16 && $bmi < 18.5) {
			$output = "	Underweight";
		} else if ($bmi >= 18.5 && $bmi < 25) {
			$output = "Normal (healthy weight)";
		} else if ($bmi >= 25 && $bmi < 30) {
			$output = "Overweight";
		} else if ($bmi >= 30 && $bmi < 35) {
			$output = "Moderately obese";
		} else if ($bmi >= 35 && $bmi < 40) {
			$output = "Severely obese";
		} else {
			$output = "Very severely obese";
		}
		return $output;
	}
	if (isset($_POST["bmi_calculate_m"])) {
		$mass = $_POST["weight"];
		$height = $_POST["height"];
		$bmi = bmi_calculate($mass, $height, "Metric");
		$weight_bmi = bmi_weight($bmi);
		echo "Yours Body Mass Index is $bmi. This is considered $weight_bmi.";
	}
	if (isset($_POST["bmi_calculate_i"])) {
		$mass = $_POST["weight"];
		$feet = $_POST["feet"];
		$inch = $_POST["inch"];
		$height = $inch + covert_feet_inch($feet);
		$bmi = bmi_calculate($mass, $height, "Imperial");
		$weight_bmi = bmi_weight($bmi);
		echo "Yours Body Mass Index is $bmi. This is considered $weight_bmi.";
	}
?>
<div>
	Metric
	<form method="post">
		<p> Weight In Kg : <input type="number"  name="weight" step="0.1" /></p>
		<p> Height in Meter: <input type="number"  name="height" step="0.01"  /></p>
		<p><input type="submit" name="bmi_calculate_m" value="Get BMI" /></p>
	</form>
	Imperial
	<form method="post">
		<p> Weight In Lbs : <input type="number"  name="weight" step="0.001" /></p>
		<p> Height: <input type="number"  name="feet" min="4" max="12" step="1"  /> Foot <input type="number"  name="inch" step="1" min="0" max="11"  /> Inch </p>
		<p><input type="submit" name="bmi_calculate_i" value="Get BMI" /></p>
	</form>
</div>

Demo

(Visited 3,035 times, 1 visits today)
Share with Friends :
Written by:

2 thoughts on “Body Mass Index (BMI) Calculator in PHP

  • 13/10/2020 at 5:27 am
    Permalink

    Adult Body Mass Index Calculator Widget Add this widget to your Web site to let anyone calculate their BMI. BMI provides a reliable indicator of body fatness for most people and is used to screen for weight categories that may lead to health problems. This calculator provides BMI and the corresponding weight category. Use this calculator for adults, 20 years old and older.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *