Unit.

Type-safe units for PHP

A lightweight, immutable library for quantities, units, and dimensional analysis. It converts automatically, blocks nonsense math, and works the moment you install it.

✓ Zero-config install ✓ Immutable & typed ✓ Affine °C/°F/K ✓ PHPStan level max

Live converter

Runs in your browser using the same factors & offsets as the PHP library. The PHP snippet updates as you type.

What it does

Small surface, real guarantees.

Automatic conversion

Add centimeters to meters and get the right answer — the library converts through a common base unit.

Dimension safety

m + s throws instead of returning garbage. Mistakes fail loudly at the call site.

Correct temperature

°C, °F, and K use affine scales (factor + offset), so conversions are accurate — not a naive multiplier.

Derived units

Multiply and divide quantities to get m/s, , and more, with dimensions tracked.

Immutable

Every operation returns a new Quantity. No hidden mutation, safe to share.

Zero dependencies

Pure PHP 8.2+, PHPStan level max, tested on 8.2/8.3/8.4.

Install

One command. Common units are ready immediately.

composer require khaledalam/unit
use KhaledAlam\Unit\Quantity;

echo Quantity::of(2, 'm')->add(Quantity::of(100, 'cm'))->to('m');  // "3 m"
echo Quantity::of(100, '°C')->to('°F');                            // "212 °F"
echo Quantity::of(4, 'm')->multiply(Quantity::of(3, 'm'));       // "12 m²"