Last active
December 12, 2015 10:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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