What are some common pitfalls when coding unit conversion scripts in PHP?

One common pitfall when coding unit conversion scripts in PHP is not handling different unit types properly, leading to incorrect conversions. To solve this, it's important to establish a clear mapping of units and their conversion factors to ensure accurate results.

function convert_units($value, $from_unit, $to_unit) {
    $conversion_factors = [
        'cm_to_inch' => 0.393701,
        'inch_to_cm' => 2.54,
        // Add more conversion factors as needed
    ];

    if(array_key_exists($from_unit . '_to_' . $to_unit, $conversion_factors)) {
        return $value * $conversion_factors[$from_unit . '_to_' . $to_unit];
    } elseif(array_key_exists($to_unit . '_to_' . $from_unit, $conversion_factors)) {
        return $value / $conversion_factors[$to_unit . '_to_' . $from_unit];
    } else {
        return 'Invalid unit conversion';
    }
}

// Example usage
echo convert_units(10, 'cm', 'inch'); // Output: 3.93701