PHPackages                             khaledalam/unit - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. khaledalam/unit

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

khaledalam/unit
===============

A lightweight, type-safe PHP library for units, quantities, and dimensional analysis with automatic conversion.

v1.3.0(1mo ago)5161[2 PRs](https://github.com/khaledalam/unit/pulls)MITPHPPHP ^8.2CI passing

Since Jul 6Pushed 1mo agoCompare

[ Source](https://github.com/khaledalam/unit)[ Packagist](https://packagist.org/packages/khaledalam/unit)[ Docs](https://github.com/khaledalam/unit)[ RSS](/packages/khaledalam-unit/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (7)Dependencies (6)Versions (8)Used By (0)

Unit
====

[](#unit)

[![Latest Stable Version](https://camo.githubusercontent.com/0d13b65a85c02ae7d605c22b6c97f2dcb4b29fbfe470c4219953cf877a5a09e1/68747470733a2f2f706f7365722e707567782e6f72672f6b68616c6564616c616d2f756e69742f76)](https://packagist.org/packages/khaledalam/unit)[![Total Downloads](https://camo.githubusercontent.com/a37d0c762e37a22a4208c1bfce3f27fd043e35b8b87583ea70a84686405e9b9b/68747470733a2f2f706f7365722e707567782e6f72672f6b68616c6564616c616d2f756e69742f646f776e6c6f616473)](https://packagist.org/packages/khaledalam/unit)[![Push](https://github.com/khaledalam/unit/actions/workflows/push.yml/badge.svg)](https://github.com/khaledalam/unit/actions/workflows/push.yml)[![codecov](https://camo.githubusercontent.com/d8a89c73941a576f62f76138fac6c76f6aefeda0eeddb70377a4e78db08a4dd9/68747470733a2f2f636f6465636f762e696f2f67682f6b68616c6564616c616d2f756e69742f67726170682f62616467652e7376673f746f6b656e3d344d494d324c52505244)](https://codecov.io/gh/khaledalam/unit)[![PHP Version](https://camo.githubusercontent.com/d4dc88fd1971839ddc94e114ad6a1c864e0b3e67e70ea37277c3eb9d76a29d8f/68747470733a2f2f706f7365722e707567782e6f72672f6b68616c6564616c616d2f756e69742f726571756972652f706870)](https://packagist.org/packages/khaledalam/unit)[![License](https://camo.githubusercontent.com/2de47a15ff05cc64e5acbe0db58b16ebf298c90bc8bd90231bc59693e34ef6d0/68747470733a2f2f706f7365722e707567782e6f72672f6b68616c6564616c616d2f756e69742f6c6963656e7365)](https://packagist.org/packages/khaledalam/unit)

### Type-safe units, quantities &amp; dimensional analysis for PHP

[](#type-safe-units-quantities--dimensional-analysis-for-php)

A lightweight, **immutable**, type-safe PHP library for working with **quantities**, **units**, and **dimensional analysis**. Define units, convert across compatible ones, and do arithmetic that refuses to add meters to seconds.

```
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')->format(1);                // "212.0 °F"
echo Quantity::of(4, 'm')->multiply(Quantity::of(3, 'm'));        // "12 m²"
```

No setup, no registration — common units are ready the moment you install.

**▶️ [Try the live demo](https://khaledalam.net/unit/)** — an interactive converter running in your browser.

---

Why Unit?
---------

[](#why-unit)

**Unit**Hand-rolled `* factor`Raw floatsBlocks nonsensical math (`m + s`)✅❌❌Auto-converts compatible units✅❌❌Affine temperature scales (°C/°F/K)✅⚠️ easy to get wrong❌Derived units (`m/s`, `m²`)✅❌❌Immutable value objects✅—❌Zero dependencies✅✅✅If you've ever shipped a bug because someone stored centimeters in a "meters" column, this library is for you.

---

Installation
------------

[](#installation)

```
composer require khaledalam/unit
```

Requires PHP 8.2+.

---

Quick start
-----------

[](#quick-start)

```
