Skip to content

Instantly share code, notes, and snippets.

@rarous
Last active August 29, 2015 14:05
Show Gist options
  • Save rarous/dc040d71e35a5a9d616b to your computer and use it in GitHub Desktop.
Save rarous/dc040d71e35a5a9d616b to your computer and use it in GitHub Desktop.
<?php namespace BodyMassIndex;
abstract class Thresholds {
const UNDERWEIGHT = 18.5;
const OVERWEIGHT = 25;
const OBESE = 30;
}
/**
* Calculates Body Mass Index.
* @param float $mass body mass [kg]
* @param float $height body height [m]
* @return float Returns calculated Body Mass Index [kg/m^2]
*/
function calc($mass, $height) {
return $mass / pow($height, 2);
}
/**
* Gets BMI category.
* @param float $bmi Body Mass Index
* @return string Returns BMI category
*/
function category($bmi) {
if ($bmi > Thresholds::OBESE) return "obese";
elseif ($bmi > Thresholds::OVERWEIGHT) return "overweight";
elseif ($bmi < Thresholds::UNDERWEIGHT) return "underweight";
else return "normal";
}
<?php
include 'bmi.php';
use BodyMassIndex as bmi;
$bmi = bmi\calc(48, 1.59);
print bmi\category($bmi) . '\n';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment