Skip to content

Instantly share code, notes, and snippets.

@kfriend
Last active December 12, 2015 10:09
Show Gist options
  • Save kfriend/4756594 to your computer and use it in GitHub Desktop.
Save kfriend/4756594 to your computer and use it in GitHub Desktop.
Simple software version comparer. Provides ability to compare an arbitrary number of version segments. Non numberic charters not supported
<?php
function compare_versions(
$version, $compareVersion, $lookup = array('greater' => 1, 'equal' => 0, 'less' => -1)
)
{
$version = explode('.', $version);
$compareVersion = explode('.', $compareVersion);
$count = max(count($version), count($compareVersion));
for ($i = 0; $i < $count; $i++)
{
if ( ! isset($version[$i])) $version[$i] = 0;
if ( ! isset($compareVersion[$i])) $compareVersion[$i] = 0;
if ($version[$i] > $compareVersion[$i])
{
return $lookup['greater'];
}
elseif ( ! isset($version[$i]) || $version[$i] < $compareVersion[$i])
{
return $lookup['less'];
}
}
return $lookup['equal'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment