PHPackages                             renfordt/clamp - 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. renfordt/clamp

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

renfordt/clamp
==============

Adds the support of the method clamp() for PHP.

v1.0.1(1y ago)17.2k—0%2MITPHPPHP ^8.2CI passing

Since Apr 14Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/renfordt/clamp)[ Packagist](https://packagist.org/packages/renfordt/clamp)[ RSS](/packages/renfordt-clamp/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (2)

clamp
=====

[](#clamp)

Adds the support of the mathematical method clamp() for PHP.

[![Badge](https://camo.githubusercontent.com/03ce95b365dc49bdab94dcb262994883ec818be776959fa39fd03ae95ea80167/687474703a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d72656e666f7264742f636c616d702d626c75652e737667)](https://github.com/renfordt/clamp)[![Packagist Version](https://camo.githubusercontent.com/d554304952b54fdd3ed0572ea885e851d429061675eea25467b9093166169d2b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72656e666f7264742f636c616d703f696e636c7564655f70726572656c6561736573)](https://packagist.org/packages/renfordt/clamp/)[![Packagist PHP Version](https://camo.githubusercontent.com/75abd1f3d20ed0aa4c14483afcdbca22de095b7f1db46573db7299c7b2f93ca2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f72656e666f7264742f636c616d702f706870)](https://camo.githubusercontent.com/75abd1f3d20ed0aa4c14483afcdbca22de095b7f1db46573db7299c7b2f93ca2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f72656e666f7264742f636c616d702f706870)[![GitHub License](https://camo.githubusercontent.com/3caac8a0cb83b5391d4ee588f2f6d403c86d0bf97cfb35fe3c63c952c6ee3c82/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f72656e666f7264742f636c616d70)](https://camo.githubusercontent.com/3caac8a0cb83b5391d4ee588f2f6d403c86d0bf97cfb35fe3c63c952c6ee3c82/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f72656e666f7264742f636c616d70)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/91ad300d23c0ad85d8cb1dee6a72362bac87eaddea5e23910fef20b55625218c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72656e666f7264742f636c616d702f74657374732e796d6c3f6c6f676f3d676974687562)](https://camo.githubusercontent.com/91ad300d23c0ad85d8cb1dee6a72362bac87eaddea5e23910fef20b55625218c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72656e666f7264742f636c616d702f74657374732e796d6c3f6c6f676f3d676974687562)[![Code Coverage](https://camo.githubusercontent.com/b4e2e77ad00a5812a5e3e545f4c3892b7f3da796b5bce644f940aa093e9924b2/68747470733a2f2f716c74792e73682f67682f72656e666f7264742f70726f6a656374732f636c616d702f636f7665726167652e737667)](https://qlty.sh/gh/renfordt/projects/clamp)[![Maintainability](https://camo.githubusercontent.com/8613f3e6733f2b54d0b2c3892d6d948508a4bb73a0bf5fd3f3c548d1430cab46/68747470733a2f2f716c74792e73682f67682f72656e666f7264742f70726f6a656374732f636c616d702f6d61696e7461696e6162696c6974792e737667)](https://qlty.sh/gh/renfordt/projects/clamp)

Important

Starting with PHP 8.6, this package will become obsolete as the `clamp()` function will be natively available in PHP. See the [PHP RFC: clamp() function v2](https://wiki.php.net/rfc/clamp_v2) for more details.

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

[](#installation)

The recommended way of installing Larvatar is to use [Composer](https://getcomposer.org/). Run the following command to install it to you project:

```
composer require renfordt/clamp

```

Usage
-----

[](#usage)

The usage is very simple and comparable to the C++ function:

```
clamp(
    $value, // The value to be clamped
    $min, // The minimum value to clamp to
    $max // The maximum value to clamp to
);
```

Alternatively you can use `clampMinMax()` which is a bit slower.

```
clampMinMax(
    $value, // The value to be clamped
    $min, // The minimum value to clamp to
    $max // The maximum value to clamp to
);
```

Why another package?
--------------------

[](#why-another-package)

Even though there are some similar packages, this one focuses on different approaches.

First of all the syntax is similar to c++ clamp function.

Secondly and more important this package focuses on performance. Other packages uses the `max($min, min($max, $num))`approach but this packages works with the following code:

```
if ($value > $max) {
    return $max;
} elseif ($value < $min) {
    return $min;
}
return $value;
```

Even though the readability is a bit worse, the performance is up to 2x faster. In most cases this is not noticeable but in some cases there will be a benefit.

Over a iteration of 100.000 executions the functions need the following times:

### String

[](#string)

- clamp: 0.0035040378570557 sec
- clampMinMax: 0.0061681270599365 sec

### Integer

[](#integer)

- clamp: 0.0029380321502686 sec
- clampMinMax: 0.0056021213531494 sec

### Float

[](#float)

- clamp: 0.0028560161590576 sec
- clampMinMax: 0.0062460899353027 sec

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance59

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~268 days

Total

2

Last Release

487d ago

PHP version history (2 changes)v1.0.0PHP ^8.1

v1.0.1PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/1edb79f2a1fcd61016ec9a5b65183e1f6e7852a4b1f9a5bd6f69c6160366f22c?d=identicon)[renfordt](/maintainers/renfordt)

---

Top Contributors

[![renfordt](https://avatars.githubusercontent.com/u/699163?v=4)](https://github.com/renfordt "renfordt (36 commits)")

---

Tags

clampphp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/renfordt-clamp/health.svg)

```
[![Health](https://phpackages.com/badges/renfordt-clamp/health.svg)](https://phpackages.com/packages/renfordt-clamp)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
