Location: PHPKode > scripts > Body Mass Index Calculator > body-mass-index-calculator/bmi.class.php
<?php
/*******************************************************************************
*   BMI-Class, for calculating one persons Body Mass Index and display
*   the result.
*   Copyright (C) 2008  Gustav Eklundh <hide@address.com>
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/

class bmi {
	# BMI Calculator v1.0, created by Gustav Eklundh
	# Useful for calculating the Body Mass Index of a person.
	public function calculate($w, $h, $m = "cm", $r = 2) {
	# double function calculate(double $w, double $h, string $m, int $r)
	# Mind the use of "." instead of "," in numbers.
	# 1.67 is correct, 1,67 isn't.
	# $r is the number of decimals used in the output.
		if(!is_numeric($w) || !is_numeric($h)) {
			throw new Exception("Unidentified weight and/or height.", 0x000001);
		}
		switch($m) {
			case "in":
				return round((($w*703)/($h*$h)), $r);
			case "ft":
				return round((($w*4.88)/($h*$h)), $r);
			case "cm":
			default:
				return round(($w/($h*$h)), $r);
		}
	}
	public function check($bmi) {
		# int function check(double $bmi)
		if(!is_numeric($bmi)) {
			throw new Exception("Unidentified BMI value", 0x000002);
		}
		if($bmi < 16.5) {
			return 1;
		}
		elseif($bmi >= 16.5 && $bmi < 18.5) {
			return 2;
		}
		elseif($bmi >= 18.5 && $bmi < 25) {
			return 3;
		}
		elseif($bmi >= 25 && $bmi < 30) {
			return 4;
		}
		elseif($bmi >= 30 && $bmi < 35) {
			return 5;
		}
		elseif($bmi >= 35 && $bmi < 40) {
			return 6;
		}
		elseif($bmi >= 40) {
			return 7;
		}
		else {
			throw new Exception("Screwed up error", 0x000003);
		}
	}
	public function translate($res) {
	# string function translate(int $res)
		$res = intval($res);
		switch($res) {
			case 1:
				return "Severely underweight";
			case 2:
				return "Underweight";
			case 3:
				return "Normal";
			case 4:
				return "Overweight";
			case 5:
				return "Obese Class 1";
			case 6:
				return "Obese Class 2";
			case 7:
				return "Obese Class 3";
			default:
				return "Error";
		}
	}
}
?>
Return current item: Body Mass Index Calculator