version
Library for handling version information and constraints
A tiny PHP library that parses semantic version numbers and checks whether a version satisfies a constraint like ^1.0 or ~1.0.0, using the same rules as the Composer package manager.
This is a small PHP library for parsing version numbers and checking whether a specific version satisfies a given constraint. It follows the semantic versioning format, where version numbers take the form major.minor.patch (for example, 2.4.1).
Version constraints are rules that describe acceptable version ranges rather than a single fixed version. This library supports the standard mathematical operators (greater than or equal to, less than or equal to, and so on) as well as two shorthand operators common in PHP tooling. The caret operator, written as ^1.0, means any version within the same major version, so it accepts 1.0.0 through 1.9.x but not 2.0.0. The tilde operator, written as ~1.0.0, is more restrictive and means any version within the same minor version, so it accepts 1.0.0 through 1.0.x but not 1.1.0. These constraint formats are the same ones used by Composer, the PHP package manager.
The library lets PHP code parse a constraint string, then check discrete version numbers against it to get a true or false result. As of version 2.0, it also handles pre-release labels like alpha or beta, and correctly orders them when comparing two versions against each other.
It is installed via Composer as a project or development dependency. The library is part of the phar-io organization, which builds tooling around the PHP Archive (PHAR) packaging format. Its high star count likely reflects its role as a widely pulled-in transitive dependency in the PHP ecosystem: many PHP projects depend on it indirectly through other packages, rather than teams choosing it directly.
Where it fits
- Check at runtime whether an installed package version satisfies a constraint like ^2.0 before running code that requires a specific API.
- Parse a version string from a config file or user input and compare two versions to determine which is newer, including pre-release labels like alpha and beta.
- Validate that the current PHP runtime meets a declared minimum version constraint before a tool or library initializes.