PHPRO.ORG

Calculating Body Mass Index (BMI) is quite simple and this small class makes it even easier. The class uses two calculations, one for imperial measurements, and one for metric, which can be set easily.

If set to metric, the weight is a measurement in kilograms and the height is a measurement in meters.

If set the imperial, the weight is a measurement in pounds and the height is a measurement in inches.

When a numeric value for the BMI has been calculated, a scale is used to detirmine where the value a lays.

Below 18.5
Underweight
18.5 - 24.9
Normal
25 - 29.9
Overweight
30 and Above
Obese

<?php
class bodyMassIndex{

    
/*
     * @string the bmi index
     */
    
public $bmi_index;

    
/*
     * @float bmi index number
     */
    
public $bmi;

    
/*
     *
     * @set variables
     *
     * @access public
     *
     * @param mixed $name
     *
     * @param mixed $value
     *
     */
    
public function __set($name$value)
    {
        switch ( 
$name )
        {
            case 
'weight':
            if(!
is_numeric($value))
            {
                throw new 
Exception"$name is Invalid" );
            }
            break;

            case 
'height':
            if(!
is_numeric($value))
            {
                throw new 
Exception"$name $value is Invalid" );
            }
            break;

            case 
'units':
            if(!
is_string($value))
            {
                throw new 
Exception"$name $value is Invalid" );
            }
            break;
            }
        
$this->$name $value;
    }

    
/**
     *
     * Returns the value of the property $name.
     *
     * @throws Exception if the property does not exist.
     *
     * @param string $name
     *
     */
    
public function __get$name )
    {
        switch ( 
$name )
        {
            case 
'height':
            return 
$this->height;
            break;

            case 
'weight':
            return 
$this->weight;
            break;

            case 
'units':
            return 
$this->units;
            break;
        }
        throw new 
Exception("Property Not Found $name");
    }


    public function 
calculateBMI()
    {
        if(
$this->units == 'imperial')
        {
            
/*** imperial ***/
            
$bmi $this->weight pow($this->height,2) * 703;
        }
        elseif(
$this->units == 'metric')
        {
            
/*** metric ***/
            
$bmi $this->weight pow($this->height,2);
        }
        else
        {
            throw new 
Exception("Invalid Unit");
        }
        
/*** assign the bmi ***/
        
$this->bmi round($bmi2);
        
$this->bmi_index $this->bmiIndex();
    }

    
/*
     * @the bmi index
     *
     * @access public
     *
     * @return string
     *
     */
    
public function bmiIndex()
    {
        if(
$this->bmi 18.5)
        {
            return 
'Underweight';
        }
        elseif(
$this->bmi <= 24.9 && $this->bmi >= 18.5)
        {
            return 
'Normal';
        }
        elseif(
$this->bmi <= 29.9 && $this->bmi >= 25)
        {
            return 
'Overweight';
        }
        elseif(
$this->bmi <= 30)
        {
            return 
'Obese';
        }
        else
        {
            throw new 
Exception("Out Of Range");
        }
    }

/*** end of class ***/

Example Usage


<?php

try
{
    
/*** a new bodyMassIndex object ***/
    
$bmi = new bodyMassIndex;

    
/*** set the units ***/
    
$bmi->units 'metric';

    
/*** height in meters ***/
    
$bmi->height 1.98;

    
/*** weight in kilograms ***/
    
$bmi->weight 78;

    
/*** calculate the bmi ***/
    
$bmi->calculateBMI();

    
/*** echo numerical index ***/
    
echo $bmi->bmi.'<br />';

    
/*** prognosis ***/
    
echo $bmi->bmi_index;
}
catch(
Exception $e)
{
    echo 
$e->getMessage();
}

?>

Demonstration

20.72
Normal