Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

version_compare

Compares two version numbers.

Syntax

version_compare(string $version1, string $version2, ?string $operator = null): int|bool

Parameters

ParameterTypeDescription
$version1stringThe first version.
$version2stringThe second version.
$operator?stringOptional. 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.

Thanks for reading!

Donate to the site — we work hard for you.