version_compare
Compares two version numbers.
Syntax
version_compare(string $version1, string $version2, ?string $operator = null): int|boolParameters
| Parameter | Type | Description |
|---|---|---|
| $version1 | string | The first version. |
| $version2 | string | The second version. |
| $operator | ?string | Optional. The operator: `<`, `<=`, `>`, `>=`, `==`. |
Return value
int|bool`-1`, `0` or `1`; with an operator, `true` or `false`.
Examples
Versions are not ordinary numbers:
<?php
var_dump(version_compare('8.10.0', '8.9.0', '>'));
var_dump('8.10.0' > '8.9.0');
var_dump(version_compare(PHP_VERSION, '8.0.0', '>='));Output:
bool(true)
bool(false)
bool(true)Notes
A plain string comparison treats 8.10 as smaller than 8.9. The current PHP version is in the PHP_VERSION constant.
See also
- function_exists — Checks whether a function is defined.
- strnatcmp — Compares strings in "natural" order — numbers inside strings as numbers.
- define — Creates a constant at run time.
Related recipes
In the tutorial
- New in PHP 8.4 — PHP 8.4 features — property hooks, asymmetric visibility, calling a method right after new, and new array functions.