You can go online and find a ton of BMI calculators all over the Internet. Their formulas seem to vary so I’m never entirely sure which one to follow (as I don’t know what formula they’re using and how accurate it is.) I decided ‘Hey! I can make one myself!’ So, I set about finding a good formula and what I kept finding was this one (at multiple reputable sites):
Body Mass Index = Weight(kg) / Height(m)2
The two, by the way, stands for squared. So, with that basic formula in mind, I set out to find how to convert kg to lbs and meters to inches (in short, the measurements I use in my daily life.) I found that, along with a wealth of other BMI formula information here. That was all of the information I needed to get started coding.
index.php
This script includes two files: index.php (which processes all of the bmi calculator stuff) and then bmicalc.css (which handles all of the visual formatting.)
The Backend
The backend of the script processes the data (lbs/kgs and inches/meters) that the user submits via a form. The data is assigned to variables. From there, the script determines which measurement system the user chose (Standard or Metric). If Standard, the values are converted into kgs and meters. From there, we use basic PHP to apply the aforementioned formula. We then announce the result along with what range the user’s BMI falls into. If Metric, the same formula is used, and the only conversions needed are to convert from centimeters to meters to fit the formula. Everything else is the same. 🙂
I made a second pass through the code and updated the conversion numbers to make the script more accurate (i.e. 0.45359237 rather than just 0.45 and 0.0254 rather than 0.025) You would be amazed at how much of a difference including the entirety of the number (rather than an abbreviated, rounded up/down iteration of the number) makes.
<?php // BMI calculator // Coded on 1/14/18, Updated on 1/15/18 to include Metric options/conversions // Used the following educational resources to determine the BMI formula: // http://extoxnet.orst.edu/faqs/dietcancer/web2/twohowto.html // http://www.medcalc.com/body.html // process data submitted in form if(isset($_POST['givems']) && ($_POST['givems'])== "Submit"){ // extract the data and assign automatically to variables extract($_POST); // now, using these variables, process the user's BMI and report it //first, determine the unit of measurement the user chose -- if Standard... if($unit == 'Standard') { // convert from lbs to kg $adjusted_weight = $weight * 0.45359237; // convert from inches to m $adjusted_height = $height * 0.0254; // square the height variable $adjusted_height_final = $adjusted_height * $adjusted_height; // divide the weight by the squared height to get the BMI value $prep_bmi = $adjusted_weight/$adjusted_height_final; $bmi = number_format($prep_bmi, 1); // and finally announce the result // center the result echo "<center>"; echo "Your BMI is $bmi. "; if($bmi > '18.5' && $bmi < '25') { echo "You are considered within normal BMI range.<br /><br />"; } elseif($bmi < '18.5') { echo "You are considered underweight.<br /><br />"; } elseif($bmi >= '25' && $bmi < '30') { echo "You are considered overweight.<br /><br />"; } elseif($bmi >= '30' && $bmi < '40') { echo "You are considered obese.<br /><br />"; } elseif($bmi >= '40') { echo "You are considered extremely obese.<br /><br />"; } // close the centering echo "</center>"; // elseif unit chosen is Metric, continue accordingly } elseif($unit=='Metric') { //convert from cm to meters to match formula $adjusted_height = $height/100; // square the height variable $adjusted_height_final = $adjusted_height * $adjusted_height; // divide the weight by the squared height to get the BMI value $prep_bmi = $weight/$adjusted_height_final; $bmi = number_format($prep_bmi, 1); // and finally announce the result // center the result echo "<center>"; echo "Your BMI is $bmi. "; if($bmi > '18.5' && $bmi < '25') { echo "You are considered within normal BMI range.<br /><br />"; } elseif($bmi < '18.5') { echo "You are considered underweight.<br /><br />"; } elseif($bmi >= '25' && $bmi < '30') { echo "You are considered overweight.<br /><br />"; } elseif($bmi >= '30' && $bmi < '40') { echo "You are considered obese.<br /><br />"; } elseif($bmi >= '40') { echo "You are considered extremely obese.<br /><br />"; } // close the centering echo "</center>"; } // close metric } // close extract post ?>
The Frontend
Just a simple form that’s been placed in a div container with some pretty basic CSS formatting.
<!doctype=html> <html> <head> <title>Kimenders.com > Simple BMI Calculator</title> <link rel="stylesheet" type="text/css" href = "bmicalc.css" /> </head> <body> <center><h2> Simple BMI Calculator </h2></center> <div id="bmiform"> <b>Please enter your measurements below. </b> <br /><br /> <form id="bmicalc" name="bmicalc" method="post"> Weight (in lbs): <input type="text" name="weight"> <br /><br /> Height (in inches): <input type="text" name="height"><br /><br /> <input type="submit" name="givems" id="givems" class="btn" value="Submit"/> </form> </div> </body> </html>
bmicalc.css
And lastly, the tiny chunk of css that provides the very basic visual styling of the script.
#bmiform { margin: auto; width: 300px; border: 3px solid silver; padding: 10px; }
And that’s it. Based on a formula that I believe to be accurate, the BMI calculator script is finished. You can test it out by checking out the demo for it here. (opens in a new window)