Converting UK drams to ounces and pounds in PHP I have written this little function to convert UK weight drams (dr) to ounces (oz) and pounds (lbs). As far as my research goes, 1 lbs = 16 oz and 1 oz = 16 dr. I need the function to return an array of lbs, oz and dr.

Am I doing it right? Or is there a better way to do it? Has my method got any flaws?

function fromDrams($drams){

    // Nullify pounds
    $ouncesAndDrams = $drams % 256;

    $lbs = (int) ($drams / 256);
    $oz  = (int) ($ouncesAndDrams / 16);
    $dr  = $ouncesAndDrams % 16;

    return ['lbs' =&gt; $lbs, 'oz' =&gt; $oz, 'dr' =&gt; $dr];
}


It does return good values when tested:

fromDrams(273) = ['lbs' =&gt; 1, 'oz' =&gt; 1, 'dr' =&gt; 1];
fromDrams(272) = ['lbs' =&gt; 1, 'oz' =&gt; 1, 'dr' =&gt; 0];
fromDrams(271) = ['lbs' =&gt; 1, 'oz' =&gt; 0, 'dr' =&gt; 15];
fromDrams(256) = ['lbs' =&gt; 1, 'oz' =&gt; 0, 'dr' =&gt; 0];
fromDrams(32) = ['lbs' =&gt; 0, 'oz' =&gt; 2, 'dr' =&gt; 0];
fromDrams(17) = ['lbs' =&gt; 0, 'oz' =&gt; 1, 'dr' =&gt; 1];
fromDrams(15) = ['lbs' =&gt; 0, 'oz' =&gt; 0, 'dr' =&gt; 15];
There are two things that stand out to me in your code. The first is that your integer cast seems a bit premature. When creating a general function like this, you probably do not want to round the answer, since 1.5 oz is a perfectly valid weight. A user of your function can then cast the resulting values if this is required.

The second is that your conversion values are magic numbers. These should be in constants with names that express the meaning of the number.

A class would be perfect to store the logic and constants. A Weight class would probably be a well-suited name. An Weight object can then hold the amount of weight in one specific unit and provide public methods to retrieve the weight in other units.

The Weight class that I have created here provides three static "Builder/Factory" methods to create a Weight object from. The constructor has been made private to support the use of the more expressive static function. The reasoning behind this, is that $weight = Weight::fromDrams(16); is a lot easier to understand than $weight = new Weight(16);.

Furthermore, the class provides the three public methods to retrieve the given weight for a specific unit.

class Weight {
    const POUNDS_TO_OUNZES = 16;
    const OUNZES_TO_DRAMS = 16;

    /**
     * @var float
     */
    private $drams;

    public static function fromPounds ($pounds) {
        return self::fromOunzes($pounds * self::POUNDS_TO_OUNZES);
    }

    public static function fromOunzes ($ounzes) {
        return self::fromDrams($ounzes * self::OUNZES_TO_DRAMS);
    }

    public static function fromDrams ($drams) {
        return new static($drams);
    }

    private function __construct ($drams) {
        $this-&gt;drams = $drams;
    }

    public function inPounds () {
        return $this-&gt;inOunzes() / self::POUNDS_TO_OUNZES;
    }

    public function inOunzes () {
        return $this-&gt;inDrams() / self::OUNZES_TO_DRAMS;
    }

    public function inDrams () {
        return $this-&gt;drams;
    }
}


If you still require a very specific function that returns an array of 'lbs', 'oz' and 'dr' like your question says, than this Weight class can be used in conjunction with it as such:

function fromDrams ($drams) {
    $weight = Weight::fromDrams($drams));

    return [
        'lbs' =&gt; (int) $weight-&gt;inPounds(),
        'oz' =&gt; (int) $weight-&gt;inOunzes(),
        'dr' =&gt; (int) $weight-&gt;inDrams(),
    ];
}


Here you see the Weight class not performing any rounding itself and letting the user of the class perform these when required.

This Weight class is now much more versatile than your original piece of code and can be used in many other situations. For example a website that provides conversion of an amount of weight for a specific unit to the respective weights for the other three units.

if (!empty($_POST["amount_in_drams"]))
    $weight = Weight::fromDrams($_POST["amount_in_drams"]);

if (!empty($_POST["amount_in_ounzes"]))
    $weight = Weight::fromOunzes($_POST["amount_in_ounzes"]);

if (!empty($_POST["amount_in_pounds"]))
    $weight = Weight::fromPounds($_POST["amount_in_pounds"]);

echo $weight-&gt;getPounds() . " pounds, ";
echo $weight-&gt;getOunzes() . " ounzes or ";
echo $weight-&gt;getDrams() . " drams.";