Tutorial: How to Write a Basal Metabolic Rate & Caloric Budget Calculator PHP Script PHP Tutorial

A New Day, A New Script
Hot on the heels of writing the Simple BMI Calculator PHP script (and its tutorial), I decided that I could really use a BMR (Basal Metabolic Rate) Calculator script that also calculates and informs me what my caloric budget would need to be every day to lose 1 pound of weight each week.

This is very useful to have if you need to lose some weight and you want a quick and easy way to figure out what your caloric budget is. If you have a good sense of how many calories you have to work with each day, you can put together a good meal plan that gives you the nourishment you need in a manner that is well suited to helping you lose about a pound each week.

Taking the Time to Make Something Better
I’ve made the formula work in Excel before, but I prefer, where possible, to use PHP as I can get around in PHP a lot more effectively than I can in Excel and given my experience is comprised much more vastly of PHP coding than of working with Excel formulas, it really is my go to language to create solutions with.

The Harris-Benedict Equation
In this script, I use multiple variations of the Harris-Benedict Equation which you’ll find here. This comes down to the fact that there are slightly different formulas for males and females, and the formulas further vary based on which measurement system you use. These are the four scenarios that this script accommodates:

– the user is using the Metric system and is female.
– the user is using the Metric system and is male.
– the user is using the Imperial system and is female.
– the user is using the Imperial system and is male.

Without Further Ado…
The script is comprised of two files: the index.php script (where all of the processing of the user’s submitted data occurs) and the bmrcalc.css file (which controls how the layout looks.)

index.php
The first chunk, as it always is, is pretty self-explanatory. It contains the name of the script, cites my name and the date it was coded, and then provides a link to the resource containing the formulas I used for the script.

// BMR calculator script
// Coded by Kim Enders 1/15/18
// Based on the Harris Benedict Equation and information found at:
// https://en.wikipedia.org/wiki/Harris%E2%80%93Benedict_equation

Now, the code for the back end truly begins. This next chunk checks to make sure that the user has submitted the form. It then extracts the data and automatically assigns it to variables

  // process data submitted in form
  if(isset($_POST['givems']) && ($_POST['givems'])== "Submit"){
  // extract the data and assign automatically to variables
  extract($_POST);

Next, we check to make sure that all of the variables containing the data that the user submitted are 1) not blank 2) contain appropriate data. We don’t want the value of $activitylevel to ever contain ‘Pick one’ as this signifies that the user forgot to pick their activity level and left the dropdown at the default message that urges the user to pick a lifestyle.

    // make sure all fields have been filled in with proper answers or are not blank
    if($age != '' && $weight != '' && $height != '' && $activitylevel != 'Pick one') { 

Now, we check to see if the user submitted standard or metric units as their preferred units of measurement

       // determine if using standard or metric

       if($unit == 'Standard') {
       // use the standard variations of the code

This next big chunk, which applies only if the user chose ‘Standard’ as their preferred unit of measurement, first checks to see whether the user has marked their gender as male or female. Since the BMR formulas are different for men and women, it’s important to have code that distinguishes between the two genders and then follows the proper code for each gender. Next, we check to see the user’s activity level. Based on their activity level, we process the last step of the formula, set the result as the $bmr variable, use that variable to find out the user’s caloric budget, then finally output to the user their basal metabolic rate as well as the caloric budget they would need to adhere to in order to lose 1 pound per week.

            // next determine exact formula based on user's gender
            if($gender == 'Female') {

                 $pre_bmr = 655.1 + (4.35*$weight) + (4.7*$height) - (4.7*$age);
        
                 // now multiply that value by the user's lifestyle, format it & announce it

                      if($activitylevel == 'Sedentary') {
                      $mult_bmr = $pre_bmr*1.2;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Light') {
                      $mult_bmr = $pre_bmr*1.375;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Moderate') {
                      $mult_bmr = $pre_bmr*1.55;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Very') {
                      $mult_bmr = $pre_bmr*1.725;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Extremely') {
                      $mult_bmr = $pre_bmr*1.9;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      }          
 
                 } elseif($gender=='Male') {

                 $pre_bmr = 066 + (6.2*$weight) + (12.7*$height) - (6.76*$age);

                 // now multiply that value by the user's lifestyle, format it & announce it

                      if($activitylevel == 'Sedentary') {
                      $mult_bmr = $pre_bmr*1.2;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Light') {
                      $mult_bmr = $pre_bmr*1.375;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Moderate') {
                      $mult_bmr = $pre_bmr*1.55;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Very') {
                      $mult_bmr = $pre_bmr*1.725;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Extremely') {
                      $mult_bmr = $pre_bmr*1.9;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      }

                 }

This chunk does the same as the previous section except it uses slightly different formulas to accommodate the metric units of measurement involved.

 } elseif($unit=='Metric') {
       // use the metric variation of the code

            // next determine exact formula based on user's gender
            if($gender=='Female') {

            $pre_bmr = 655.1 + (9.563*$weight) + (1.850*$height) - (4.676*$age);
        
                 // now multiply that value by the user's lifestyle, format it & announce it

                      if($activitylevel == 'Sedentary') {
                      $mult_bmr = $pre_bmr*1.2;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Light') {
                      $mult_bmr = $pre_bmr*1.375;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Moderate') {
                      $mult_bmr = $pre_bmr*1.55;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Very') {
                      $mult_bmr = $pre_bmr*1.725;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Extremely') {
                      $mult_bmr = $pre_bmr*1.9;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      }          
 
                 } elseif($gender=='Male') {

                 $pre_bmr = 66.5 + (13.75*$weight) + (5.003*$height) - (6.755*$age);

                 // now multiply that value by the user's lifestyle, format it & announce it

                      if($activitylevel == 'Sedentary') {
                      $mult_bmr = $pre_bmr*1.2;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Light') {
                      $mult_bmr = $pre_bmr*1.375;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Moderate') {
                      $mult_bmr = $pre_bmr*1.55;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Very') {
                      $mult_bmr = $pre_bmr*1.725;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      } elseif($activitylevel == 'Extremely') {
                      $mult_bmr = $pre_bmr*1.9;
                      $bmr = number_format($mult_bmr,0);
                      $lose1 = number_format($mult_bmr - 500);
                      echo "<center>Your Basal Metabolic Rate is $bmr calories a day. <br /> To lose one pound a week, your caloric budget would be $lose1 calories every day.</center><br /><br />";

                      }  // elseif activity level
 
                 } // elseif gender = male
   
       } // elseif unit is metric

This next section is the elseif continuation of a much earlier chunk of code in this script. The earlier portion of code works by asking: “Are the fields not blank and are they also appropriate?” If yes, the earlier on code executes the code that follows it. The elseif portion, on the other hand, first says “if the $activitylevel variable holds ‘Pick one’, then output this error message. It does this to make sure that the user doesn’t forget to pick an activity level (rather than forget and submit the default message instructing them to pick an activity level.) Then it says ‘If any of these other variables are blank, then output this message instead.”

After that, all that remains for the PHP portion of the script is the closing bracket for the earlier code that checks to see if the post was successfully submitted and the closing php bracket.

    // handling form submission errors/inappropriate answers
    } elseif($activitylevel == 'Pick one') {

      echo "Please first select an activity level!";

    } elseif($age == '' || $weight =='' || $height == '') {

      echo "Please make sure all fields have been entered or selected!"; 
    }
   
  } // if post is submitted

?> 

After the PHP portion of the script is the HTML portion which contains the code for the front end of the script (what your user will see and interact with.) More precisely, it contains the form that the user interacts with. This form asks for their preferred unit of measurement, gender, age, weight, height, and activity level.

<!doctype html>
<html>
<head>
<title> KimEnders.com > BMR Calculator script</title>
<link rel="stylesheet" type="text/css" href = "bmrcalc.css" />

</head>

<body>

<center><h3>BMR and Caloric Budget Calculator</h3></center>

<div id="bmrbox">

<form id="bmrform" name="bmrform" method="post">

Select unit: <input type="radio" name="unit" value="Standard" checked> Standard
             <input type="radio" name="unit" value="Metric"> Metric <br /><br />

Gender: <input type="radio" name="gender" value="Male" checked> Male
        <input type="radio" name="gender" value="Female"> Female <br /><br />

Age: <input type="text" name="age" placeholder="Your Age"> <br /><br />

Weight: <input type="text" name="weight" placeholder="Your weight (in kg or lbs)"><br /><br />
Height: <input type="text" name="height" placeholder="Your height (in cm or inches)" size="22"><br /><br />

Your activity level:<br />
<select name="activitylevel" id="sel">
<option value="Pick one" checked> Pick an activity level!</option>
<option value="Sedentary"> Sedentary: Little to no exercise</option>
<option value="Light"> Light exercise: one to three days a week</option>
<option value="Moderate"> Moderately active: three to five days a week</option>
<option value="Very">Very active: vigorous exercise six to seven days a week</option>
<option value="Extremely">Extremely active: intense exercise six to seven days a week</option>
</select><br /><br />

<input type="submit" name="givems" id="givems" class="btn" value="Submit"/>
</form>

</div>

</body>
</html>

bmrcalc.css
Another very basically styled script, the css file for it doesn’t contain a lot of code. What code it does contain dictates what the div container containing the user input form will look like as well as the size of the dropdown menu within it.

#bmrform {
    margin: auto;
    width: 300px;
    border: 3px solid silver;
    padding: 10px;
}

#sel{
 width:150px;   
}

And that’s it. If you’d like to try the script out, you can check out the demo here.

Wrapping Up
I spent a while debugging this script. It’s easy to be scatterbrained and make goofy mistakes like forgetting to add closing brackets. Sometimes the bugs I find are simply due to certain code sequences not working the way I expect them to. (It’s always just lovely to find limitations within a coding language that you weren’t even aware exist.) At any rate, I do hope that you find this script and the tutorial useful. 🙂

If you found this post useful, leave a comment below!

Leave a Reply

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