Skip to content

Instantly share code, notes, and snippets.

@allendav
Last active November 10, 2021 18:57
Show Gist options
  • Save allendav/bc36b9241aa33539eafd93a1c5e99ff4 to your computer and use it in GitHub Desktop.
Save allendav/bc36b9241aa33539eafd93a1c5e99ff4 to your computer and use it in GitHub Desktop.
PHP Version Compare Test
<?php
$version_tests = [
[
"minimum" => "2.0",
"found" => "2.0",
"meets" => true
],
[
"minimum" => "2.0",
"found" => "2.0.0",
"meets" => true
],
[
"minimum" => "2.0.0",
"found" => "2.0.0",
"meets" => true
],
[
"minimum" => "2.0.0",
"found" => "2.0.0RC1",
"meets" => false
],
[
"minimum" => "2.0.0",
"found" => "2.0.1RC1",
"meets" => true
],
[
"minimum" => "2.0.1",
"found" => "2.0.RC1",
"meets" => false
],
[
"minimum" => "2.0.1",
"found" => "2.0.pl",
"meets" => true // https://stackoverflow.com/questions/18491729/what-does-pl-or-p-mean-in-a-version-number
],
];
foreach ($version_tests as $version_test) {
$comparison_result = version_compare($version_test["minimum"], $version_test["found"]);
$meets = $comparison_result == -1 || $comparison_result == 0;
if ($meets == $version_test["meets"]) {
echo "PASS\n";
} else {
echo "FAIL {$version_test['minimum']} {$version_test['found']}\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment