version_compare
version_compare(string $version1, string $version2, ?string $operator = null): int|bool
Compares two version strings numerically, recognizing dev/a/b/RC/pl/p suffixes; with a third operator argument it returns a bool directly.
C C23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* No semver in the standard library; minimal hand-written comparison: numeric per segment + prerelease < release */
static int cmp_ver(const char *a, const char *b) {
char ca[64], cb[64], pa[64] = "", pb[64] = "";
const char *dash = strchr(a, '-');
snprintf(ca, sizeof ca, "%.*s", dash ? (int)(dash - a) : (int)strlen(a), a);
if (dash) snprintf(pa, sizeof pa, "%s", dash + 1);
dash = strchr(b, '-');
snprintf(cb, sizeof cb, "%.*s", dash ? (int)(dash - b) : (int)strlen(b), b);
if (dash) snprintf(pb, sizeof pb, "%s", dash + 1);
/* Numeric per-segment comparison (missing segments count as 0; the string "10" compares numerically > "9") */
const char *qa = ca, *qb = cb;
while (*qa || *qb) {
int x = *qa ? (int)strtol(qa, (char **)&qa, 10) : 0;
int y = *qb ? (int)strtol(qb, (char **)&qb, 10) : 0;
if (x != y) return x > y ? 1 : -1;
if (*qa == '.') qa++;
if (*qb == '.') qb++;
}
if (strcmp(pa, pb) == 0) return 0;
if (pa[0] == '\0') return 1; /* No prerelease > with prerelease */
if (pb[0] == '\0') return -1;
return strcmp(pa, pb) > 0 ? 1 : -1;
}
int main(void) {
printf("1.10.0 vs 1.9.0 = %d\n", cmp_ver("1.10.0", "1.9.0"));
printf("10.0.0 vs 9.0.0 = %d\n", cmp_ver("10.0.0", "9.0.0"));
printf("1.0.1 vs 1.0.0 = %d\n", cmp_ver("1.0.1", "1.0.0"));
printf("1.0.0-beta vs 1.0.0 = %d\n", cmp_ver("1.0.0-beta", "1.0.0"));
printf("2.0.0 vs 2.0.0 = %d\n", cmp_ver("2.0.0", "2.0.0"));
return 0;
}
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
C++ C++26
#include <iostream>
#include <string>
// No semver in the standard library; minimal hand-written comparison: numeric per segment + prerelease < release
static int cmp_ver(const std::string &a, const std::string &b) {
auto split_pre = [](const std::string &v) {
size_t dash = v.find('-');
return dash == std::string::npos ? std::make_pair(v, std::string())
: std::make_pair(v.substr(0, dash), v.substr(dash + 1));
};
auto [ca, pa] = split_pre(a);
auto [cb, pb] = split_pre(b);
// Numeric per-segment comparison (missing segments count as 0; "10" compares numerically > "9")
size_t qa = 0, qb = 0;
while (qa < ca.size() || qb < cb.size()) {
int x = 0, y = 0;
if (qa < ca.size()) {
size_t nx;
x = std::stoi(ca.substr(qa), &nx);
qa += nx + (qa + nx < ca.size() ? 1 : 0);
}
if (qb < cb.size()) {
size_t nx;
y = std::stoi(cb.substr(qb), &nx);
qb += nx + (qb + nx < cb.size() ? 1 : 0);
}
if (x != y) return x > y ? 1 : -1;
}
if (pa == pb) return 0;
if (pa.empty()) return 1;
if (pb.empty()) return -1;
return pa > pb ? 1 : -1;
}
int main() {
std::cout << "1.10.0 vs 1.9.0 = " << cmp_ver("1.10.0", "1.9.0") << "\n";
std::cout << "10.0.0 vs 9.0.0 = " << cmp_ver("10.0.0", "9.0.0") << "\n";
std::cout << "1.0.1 vs 1.0.0 = " << cmp_ver("1.0.1", "1.0.0") << "\n";
std::cout << "1.0.0-beta vs 1.0.0 = " << cmp_ver("1.0.0-beta", "1.0.0") << "\n";
std::cout << "2.0.0 vs 2.0.0 = " << cmp_ver("2.0.0", "2.0.0") << "\n";
return 0;
}
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
C# v11.0.100
// The standard library's System.Version only parses numeric segments (no -beta awareness); prerelease needs hand-writing or the Semver package
static int CmpVer(string a, string b)
{
var ca = a.Split('-');
var cb = b.Split('-');
var pa = ca[0].Split('.');
var pb = cb[0].Split('.');
int n = Math.Max(pa.Length, pb.Length);
for (int i = 0; i < n; i++)
{
int x = i < pa.Length ? int.Parse(pa[i]) : 0;
int y = i < pb.Length ? int.Parse(pb[i]) : 0;
if (x != y) return x > y ? 1 : -1;
}
string preA = ca.Length > 1 ? ca[1] : "";
string preB = cb.Length > 1 ? cb[1] : "";
if (preA == preB) return 0;
if (preA == "") return 1;
if (preB == "") return -1;
return string.CompareOrdinal(preA, preB) > 0 ? 1 : -1;
}
Console.WriteLine($"1.10.0 vs 1.9.0 = {CmpVer("1.10.0", "1.9.0")}");
Console.WriteLine($"10.0.0 vs 9.0.0 = {CmpVer("10.0.0", "9.0.0")}");
Console.WriteLine($"1.0.1 vs 1.0.0 = {CmpVer("1.0.1", "1.0.0")}");
Console.WriteLine($"1.0.0-beta vs 1.0.0 = {CmpVer("1.0.0-beta", "1.0.0")}");
Console.WriteLine($"2.0.0 vs 2.0.0 = {CmpVer("2.0.0", "2.0.0")}");
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
Go v1.27.4
package main
import (
"fmt"
"strconv"
"strings"
)
// No semver in the standard library (golang.org/x/mod/semver is idiomatic); minimal hand-written comparison: numeric per segment + prerelease < release
func cmpVer(a, b string) int {
coreA, preA := splitPre(a)
coreB, preB := splitPre(b)
partsA := strings.Split(coreA, ".")
partsB := strings.Split(coreB, ".")
n := len(partsA)
if len(partsB) > n {
n = len(partsB)
}
for i := 0; i < n; i++ {
x, y := 0, 0
if i < len(partsA) {
x, _ = strconv.Atoi(partsA[i])
}
if i < len(partsB) {
y, _ = strconv.Atoi(partsB[i])
}
if x != y {
if x > y {
return 1
}
return -1
}
}
if preA == preB {
return 0
}
if preA == "" {
return 1
}
if preB == "" {
return -1
}
if preA > preB {
return 1
}
return -1
}
func splitPre(v string) (string, string) {
if i := strings.Index(v, "-"); i >= 0 {
return v[:i], v[i+1:]
}
return v, ""
}
func main() {
fmt.Println("1.10.0 vs 1.9.0 =", cmpVer("1.10.0", "1.9.0"))
fmt.Println("10.0.0 vs 9.0.0 =", cmpVer("10.0.0", "9.0.0"))
fmt.Println("1.0.1 vs 1.0.0 =", cmpVer("1.0.1", "1.0.0"))
fmt.Println("1.0.0-beta vs 1.0.0 =", cmpVer("1.0.0-beta", "1.0.0"))
fmt.Println("2.0.0 vs 2.0.0 =", cmpVer("2.0.0", "2.0.0"))
}
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
Java v27.0.1
public class Main {
// No semver in the standard library; minimal hand-written comparison: numeric per segment + prerelease < release
static int cmpVer(String a, String b) {
String coreA = a, preA = "";
String coreB = b, preB = "";
int ia = a.indexOf('-');
int ib = b.indexOf('-');
if (ia >= 0) { coreA = a.substring(0, ia); preA = a.substring(ia + 1); }
if (ib >= 0) { coreB = b.substring(0, ib); preB = b.substring(ib + 1); }
String[] pa = coreA.split("\\.");
String[] pb = coreB.split("\\.");
int n = Math.max(pa.length, pb.length);
for (int i = 0; i < n; i++) {
int x = i < pa.length ? Integer.parseInt(pa[i]) : 0;
int y = i < pb.length ? Integer.parseInt(pb[i]) : 0;
if (x != y) return x > y ? 1 : -1;
}
if (preA.equals(preB)) return 0;
if (preA.isEmpty()) return 1;
if (preB.isEmpty()) return -1;
return preA.compareTo(preB) > 0 ? 1 : -1;
}
public static void main(String[] args) {
System.out.println("1.10.0 vs 1.9.0 = " + cmpVer("1.10.0", "1.9.0"));
System.out.println("10.0.0 vs 9.0.0 = " + cmpVer("10.0.0", "9.0.0"));
System.out.println("1.0.1 vs 1.0.0 = " + cmpVer("1.0.1", "1.0.0"));
System.out.println("1.0.0-beta vs 1.0.0 = " + cmpVer("1.0.0-beta", "1.0.0"));
System.out.println("2.0.0 vs 2.0.0 = " + cmpVer("2.0.0", "2.0.0"));
}
}
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
JavaScript v26.13.1
// No standard-library semver (npm's semver package is idiomatic); minimal hand-written comparison: numeric per segment + prerelease < release
function cmpVer(a, b) {
const [coreA, preA] = a.split("-");
const [coreB, preB] = b.split("-");
const va = coreA.split(".").map(Number);
const vb = coreB.split(".").map(Number);
const n = Math.max(va.length, vb.length);
for (let i = 0; i < n; i++) {
const x = va[i] ?? 0;
const y = vb[i] ?? 0;
if (x !== y) return x > y ? 1 : -1;
}
if (preA === preB) return 0;
if (preA === undefined) return 1;
if (preB === undefined) return -1;
return preA > preB ? 1 : -1;
}
console.log(`1.10.0 vs 1.9.0 = ${cmpVer("1.10.0", "1.9.0")}`);
console.log(`10.0.0 vs 9.0.0 = ${cmpVer("10.0.0", "9.0.0")}`);
console.log(`1.0.1 vs 1.0.0 = ${cmpVer("1.0.1", "1.0.0")}`);
console.log(`1.0.0-beta vs 1.0.0 = ${cmpVer("1.0.0-beta", "1.0.0")}`);
console.log(`2.0.0 vs 2.0.0 = ${cmpVer("2.0.0", "2.0.0")}`);
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
PHP v8.6.0
// version_compare: number-aware comparison ("1.10" > "1.9"), recognizing the special strings dev<a<b<RC<#<pl/p
echo version_compare("1.10.0", "1.9.0"), "\n";
echo version_compare("10.0.0", "9.0.0"), "\n";
// A PHP specialty: special strings like RC/pl participate in comparison directly (release > RC)
echo version_compare("1.0.1", "1.0RC1"), "\n";
// Prerelease < release
echo version_compare("1.0.0-beta", "1.0.0"), "\n";
echo version_compare("2.0.0", "2.0.0"), "\n";
// Pass an operator as the 3rd argument to get a bool directly
echo version_compare("1.0.0", "0.9.9", ">=") ? "yes" : "no";
1 1 1 -1 0 yes
Python v3.15.2
# No semver in the standard library (packaging.version is idiomatic in real projects); minimal hand-written comparison: numeric per segment + prerelease < release
def cmp_ver(a, b):
core_a, _, pre_a = a.partition("-")
core_b, _, pre_b = b.partition("-")
va = [int(x) for x in core_a.split(".")]
vb = [int(x) for x in core_b.split(".")]
n = max(len(va), len(vb))
va += [0] * (n - len(va))
vb += [0] * (n - len(vb))
if va != vb:
return 1 if va > vb else -1
if pre_a == pre_b:
return 0
if pre_a == "":
return 1
if pre_b == "":
return -1
return 1 if pre_a > pre_b else -1
print("1.10.0 vs 1.9.0 =", cmp_ver("1.10.0", "1.9.0"))
print("10.0.0 vs 9.0.0 =", cmp_ver("10.0.0", "9.0.0"))
print("1.0.1 vs 1.0.0 =", cmp_ver("1.0.1", "1.0.0"))
print("1.0.0-beta vs 1.0.0 =", cmp_ver("1.0.0-beta", "1.0.0"))
print("2.0.0 vs 2.0.0 =", cmp_ver("2.0.0", "2.0.0"))
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
Rust v1.99.1
use std::convert::TryInto;
// No semver in the standard library (the semver crate is idiomatic in real projects); minimal hand-written comparison: numeric per segment + prerelease < release
fn cmp_ver(a: &str, b: &str) -> i32 {
let (core_a, pre_a) = match a.split_once('-') {
Some((c, p)) => (c, p),
None => (a, ""),
};
let (core_b, pre_b) = match b.split_once('-') {
Some((c, p)) => (c, p),
None => (b, ""),
};
let pa: Vec<i64> = core_a.split('.').map(|x| x.parse().unwrap()).collect();
let pb: Vec<i64> = core_b.split('.').map(|x| x.parse().unwrap()).collect();
let n = pa.len().max(pb.len());
for i in 0..n {
let x = pa.get(i).copied().unwrap_or(0);
let y = pb.get(i).copied().unwrap_or(0);
if x != y {
return if x > y { 1 } else { -1 };
}
}
if pre_a == pre_b {
0
} else if pre_a.is_empty() {
1
} else if pre_b.is_empty() {
-1
} else if pre_a > pre_b {
1
} else {
-1
}
}
fn main() {
println!("1.10.0 vs 1.9.0 = {}", cmp_ver("1.10.0", "1.9.0"));
println!("10.0.0 vs 9.0.0 = {}", cmp_ver("10.0.0", "9.0.0"));
println!("1.0.1 vs 1.0.0 = {}", cmp_ver("1.0.1", "1.0.0"));
println!("1.0.0-beta vs 1.0.0 = {}", cmp_ver("1.0.0-beta", "1.0.0"));
println!("2.0.0 vs 2.0.0 = {}", cmp_ver("2.0.0", "2.0.0"));
}
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
TypeScript v26.13.1
// No standard-library semver (npm's semver package is idiomatic); minimal hand-written comparison: numeric per segment + prerelease < release
function cmpVer(a: string, b: string): number {
const [coreA, preA] = a.split("-");
const [coreB, preB] = b.split("-");
const va = coreA.split(".").map(Number);
const vb = coreB.split(".").map(Number);
const n = Math.max(va.length, vb.length);
for (let i = 0; i < n; i++) {
const x = va[i] ?? 0;
const y = vb[i] ?? 0;
if (x !== y) return x > y ? 1 : -1;
}
if (preA === preB) return 0;
if (preA === undefined) return 1;
if (preB === undefined) return -1;
return preA > preB ? 1 : -1;
}
console.log(`1.10.0 vs 1.9.0 = ${cmpVer("1.10.0", "1.9.0")}`);
console.log(`10.0.0 vs 9.0.0 = ${cmpVer("10.0.0", "9.0.0")}`);
console.log(`1.0.1 vs 1.0.0 = ${cmpVer("1.0.1", "1.0.0")}`);
console.log(`1.0.0-beta vs 1.0.0 = ${cmpVer("1.0.0-beta", "1.0.0")}`);
console.log(`2.0.0 vs 2.0.0 = ${cmpVer("2.0.0", "2.0.0")}`);
1.10.0 vs 1.9.0 = 1 10.0.0 vs 9.0.0 = 1 1.0.1 vs 1.0.0 = 1 1.0.0-beta vs 1.0.0 = -1 2.0.0 vs 2.0.0 = 0
Language differences
C: no semver library — hand-write per-segment strtol comparisons; embedded projects usually roll their own.
C++: same as C; std::stoi per segment covers the common cases.
C#: System.Version.Parse only handles numeric segments (1.2.3.4), no prerelease suffix; use the Semver NuGet package for SemVer; the example hand-writes -beta support.
Go: golang.org/x/mod/semver is the idiom (Go module versions are SemVer); the example hand-writes equivalent logic for comparison.
Java: nothing in the standard library; hand-write per-segment comparison or use maven-artifact's ComparableVersion.
JavaScript: no standard-library semver (the npm semver package is the norm); when hand-writing, compare segments numerically via Number() — not as strings.
PHP: version_compare is numerically aware and orders special strings (dev < a < alpha < b < beta < RC < rc < # < pl < p); the third operator argument (<, <=, >, >=, ==, !=) returns a bool directly; comparing 1.0.1 with 1.0RC1 gives 1 (release > RC).
Python: nothing in std (packaging.version.Version is the de-facto standard in the pip ecosystem); the example hand-writes numeric segments + prerelease < release.
Rust: nothing in std (the semver crate is the norm); the example hand-writes equivalent logic.
TypeScript: same as JavaScript.