PHPackages                             kherge/semver - 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. kherge/semver

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

kherge/semver
=============

Manages semantic version numbers and compares them.

1.0.0(9y ago)64.3k↓93.3%1MITPHPPHP &gt;=7.0

Since Oct 5Pushed 6y ago2 watchersCompare

[ Source](https://github.com/kherge/php.semver)[ Packagist](https://packagist.org/packages/kherge/semver)[ RSS](/packages/kherge-semver/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (4)Used By (0)

[![Build Status](https://camo.githubusercontent.com/c1913821daeb81c9b42d5db840b17d5a8fba56c50d27a801078b515b06f30792/68747470733a2f2f7472617669732d63692e6f72672f6b68657267652f7068702e73656d7665722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kherge/php.semver)[![Packagist](https://camo.githubusercontent.com/dd0bf9cc6481576d126e3394ea542f06a3ce10f301680c6331a861cfce6179c1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b68657267652f73656d7665722e737667)](https://packagist.org/packages/kherge/semver)[![Packagist Pre Release](https://camo.githubusercontent.com/cc56e92be794b4e16048338e10eafcdfb63460ca5f47df98d556922e9c42a5f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6b68657267652f73656d7665722e737667)](https://packagist.org/packages/kherge/semver)

Version
=======

[](#version)

A library for parsing and comparing semantic version numbers.

Usage
-----

[](#usage)

Please note that full qualified names are not used in the examples below. All references to interfaces, classes, and functions can be found in the namespace `KHerGe\Version`. Also note that the examples below do not demonstrate the complete abilities of the library. Please refer to the source files for more information.

### Simple

[](#simple)

#### Parsing

[](#parsing)

For very simple use cases, you only need the `parse()` function.

```
use function KHerGe\Version\parse;
```

With this function, you can create a value object that represents an individual semantic version number. The value object is immutable, but convenience methods are available so that you can alter values and receive new value objects.

```
// Create a new value object.
$version = parse('1.2.3-alpha.1+20161004');

// Bump the patch number: 1.0.1
$patched = $version->incrementPatch();

// The original value object is unchanged.
echo $version; // 1.2.3-alpha.1+20161004

// But the patched version number has the change.
echo $patched; // 1.2.4
```

#### Comparing

[](#comparing)

Simple comparisons can also be performed directly on the value objects.

```
if ($patched->isGreaterThan($version)) {
    // $patched is greater than $version
}
```

#### Validating

[](#validating)

While, the `parse()` function throws a `InvalidStringRepresentationException`for invalid string representations of a semantic version number, you can still check yourself by using the `is_valid()` function.

```
use function KHerGe\Version\is_valid;

$version = '1.2.3-alpha.1+20161004';

if (is_valid($version)) {
    // $version is valid
}
```

### Complex

[](#complex)

#### Implementations

[](#implementations)

The library will work on any implementation of `VersionInterface` but provides a `Version` implementation that includes a lot of extra methods for convenience.

```
$version = new Version(

    // major
    1,

    // minor
    2,

    // patch
    3,

    // pre-release
    ['a', 'b', 'c'],

    // build
    ['x', 'y', 'z']

);
```

#### Parsing

[](#parsing-1)

If you need to use your own implementation of `VersionInterface`, the library provides a function to parse the components of a string representation so that you won't have to.

```
use function KHerGe\Version\parse_components;

$components = parse_components('1.2.3-alpha.1+20161004');
```

The result of `parse_components()` can be used to create a new instance that implements `VersionInterface`. This function performs its own validation, so checking with `is_valid()` will be redundant.

```
$components = [
    'major' => 1,
    'minor' => 2,
    'patch' => 3,
    'pre-release' => ['alpha', '1'],
    'build' => ['20161004']
];
```

#### Comparing

[](#comparing-1)

The library contains a set of pre-made constraints, all of which implement `ConstraintInterface`. These constraints can be mixed and matched in order to perform far more complex comparison operations than by using the constraints on their own.

```
use KHerGe\Version\Compare\Constraint\AndX;
use KHerGe\Version\Compare\Constraint\EqualTo;
use KHerGe\Version\Compare\Constraint\GreaterThan;
use KHerGe\Version\Compare\Constraint\LessThan;
use KHerGe\Version\Compare\Constraint\OrX;

use function KHerGe\Version\parse;

// Match any of the following constraints.
$constraint = new OrX(
    [
        // Match all of the following constraints.
        new AndX(
            [
                // Must be greater than "0.2.3".
                new GreaterThan(parse('0.2.3')),

                // Must be less than "0.4.4".
                new LessThan(parse('0.4.4')),
            ]
        ),

        // Match exactly "0.4.5".
        new EqualTo(parse('0.4.5'))
    ]
);

// Verify that the version meets the constraints.
$version = parse('0.4.0');

if ($constraint->allows($version)) {
    // $version is allowed
}
```

Requires
--------

[](#requires)

- PHP 7.0 or greater

Install
-------

[](#install)

To install, you will need to use Composer.

```
composer require kherge/semver

```

License
-------

[](#license)

Released under both MIT and Apache 2.0.

See [LICENSE](LICENSE).

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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 ~59 days

Total

3

Last Release

3441d ago

Major Versions

0.2.0 → 1.0.02017-02-01

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9122157?v=4)[Kevin Herrera](/maintainers/kherge)[@kherge](https://github.com/kherge)

---

Top Contributors

[![kherge](https://avatars.githubusercontent.com/u/9122157?v=4)](https://github.com/kherge "kherge (42 commits)")

---

Tags

semverversioningsemverversioningsemantic

### Embed Badge

![Health badge](/badges/kherge-semver/health.svg)

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

###  Alternatives

[composer/semver

Version comparison library that offers utilities, version constraint parsing and validation.

3.3k522.3M994](/packages/composer-semver)[nikolaposa/version

Value Object that represents a SemVer-compliant version number.

1407.3M23](/packages/nikolaposa-version)[shivas/versioning-bundle

Symfony application versioning, simple console command to manage version (with providers e.g. git tag) of your application using Semantic Versioning 2.0.0 recommendations

1111.3M1](/packages/shivas-versioning-bundle)[wenzhixin/bootstrap-table

An extended table to integration with some of the most widely used CSS frameworks. (Supports Bootstrap, Semantic UI, Bulma, Material Design, Foundation)

11.8k287.3k1](/packages/wenzhixin-bootstrap-table)[naneau/semver

A decent, standards-compliant, Semantic Versioning (SemVer) parser and library

73522.7k18](/packages/naneau-semver)[pragmarx/version

Take control over your Laravel app version

5931.2M2](/packages/pragmarx-version)

PHPackages © 2026

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