Download the PHP package khaledalam/unit without Composer

On this page you can find all versions of the php package khaledalam/unit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package unit

Unit

Latest Stable Version Total Downloads Push codecov PHP Version License

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.

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

▶️ Try the live demo — an interactive converter running in your browser.


Why Unit?

Unit Hand-rolled * factor Raw floats
Blocks 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

Requires PHP 8.2+.


Quick start


Features


Usage

Conversion

Arithmetic

All operations return new Quantity objects with the proper dimension and unit.

Scalar math

Dimension names

Temperature (affine scales)

Parsing strings

Humanize (auto-pick the best unit)

Comparison

Precision & formatting

JSON

Custom units

Register your own units against a dimension:

Incompatible operations throw


Supported units

Dimension Units
Length mm, cm, m, km, in, ft, yd, mi
Mass mg, g, kg, t, lb, oz
Time ms, s, min, h, d, wk
Area cm², m², km², ft², ha, acre
Volume mL, L, m³, gal
Temperature °C, °F, K
Speed m/s, km/h, mph, kn
Data bit, B, KB, MB, GB, TB, KiB, MiB, GiB, TiB
Force N, kN
Energy J, kJ, cal, kcal, Wh, kWh
Power W, kW, hp
Pressure Pa, kPa, bar, atm, psi
Frequency Hz, kHz, MHz, GHz
Angle rad, deg, grad, turn

Need more? Open an issue or register your own.


Laravel integration

The package ships a service provider (auto-discovered) and an Eloquent cast. Store a quantity in a single column and get a Quantity back on access:

Register app-specific units via config/unit.php:

Requires illuminate/database (already present in any Laravel app).


Symfony / Doctrine integration

A Doctrine DBAL type persists a Quantity as a string column. Register it once, then map any column with it:

Strings assigned to the property are parsed on write, and the column hydrates back into a Quantity on read.

Requires doctrine/dbal ^4.


Examples

Runnable scripts live in examples/:


Testing & quality

The suite combines PHPUnit test classes with .phpt scenario tests and holds 100% line coverage, verified on PHP 8.2, 8.3, and 8.4 in CI (PHPStan level max, PSR-12).


Contributing

Contributions are welcome! Please read CONTRIBUTING.md and the Code of Conduct. Adding a unit is usually a three-line change.


License

MIT © Khaled Alam


All versions of unit with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package khaledalam/unit contains the following files

Loading the files please wait ...