Units add-on

Expressions with
something to measure.

Use explicit quantities in metres, areas, litres, mass, time, temperature, angles, and speeds. The add-on converts compatible values before arithmetic and rejects dimension mistakes.

Start with a quantity

Install the private package, then evaluate the expression through UnitMath:

$quantity = UnitMath::evaluate('2m * 6 + 200cm');
// 14 m

Convert when the output needs a specific unit

Use the explicit to operator to change the display unit while keeping the normalized value:

$distance = UnitMath::evaluate('25m to km');
// 0.025 km

Derived quantities work the same way

Square and cubic lengths, litres, and registered speeds share dimensions, so compatible values can be combined or converted:

UnitMath::evaluate('1L + 500mL'); // 1.5 L
UnitMath::evaluate('60mph to kmh'); // 96.56064 kmh

Conversions happen before addition

200cm is normalized to two metres, so it can be added to 12m. Adding 2m + 3s fails with the stable units.incompatible_addition error instead of returning a meaningless number.

Temperature offsets stay explicit

Absolute readings such as 20C and 10C cannot be added. Subtracting them gives a Kelvin difference: 20C - 10C returns 10 K; convert with 0C to F for 32 F.

Keep the model in your API

A quantity serializes as its normalized numeric value, display unit, dimensions, and formatted label. Pass that object to the explaining add-on to produce translated unit steps, or to the visuals add-on for unit-labelled axes.

Scope:The core mathphp/mathphp package remains scalar-only. Units are an opt-in grammar and dependency.
See the package details →