note 89416 added to function.version-compare

From: Date: Fri, 06 Mar 2009 21:05:42 +0000
Subject: note 89416 added to function.version-compare
Groups: php.notes 
Request: Send a blank email to php-notes+get-151571@lists.php.net to get a copy of this message
Since this function considers 1 < 1.0 < 1.0.0, others might find this function useful (which considers 1 == 1.0): //Compare two sets of versions, where major/minor/etc. releases are separated by dots. //Returns 0 if both are equal, 1 if A > B, and -1 if B < A. function version_compare2($a, $b) { $a = explode(".", rtrim($a, ".0")); //Split version into pieces and remove trailing .0 $b = explode(".", rtrim($b, ".0")); //Split version into pieces and remove trailing .0 foreach ($a as $depth => $aVal) { //Iterate over each piece of A if (isset($b[$depth])) { //If B matches A to this depth, compare the values if ($aVal > $b[$depth]) return 1; //Return A > B else if ($aVal < $b[$depth]) return -1; //Return B > A //An equal result is inconclusive at this point } else { //If B does not match A to this depth, then A comes after B in sort order return 1; //so return A > B } } //At this point, we know that to the depth that A and B extend to, they are equivalent. //Either the loop ended because A is shorter than B, or both are equal. return (count($a) < count($b)) ? -1 : 0; } ---- Server IP: 64.71.164.2 Probable Submitter: 143.44.175.254 ---- Manual Page -- http://www.php.net/manual/en/function.version-compare.php Edit -- https://master.php.net/note/edit/89416 Del: integrated -- https://master.php.net/note/delete/89416/integrated Del: useless -- https://master.php.net/note/delete/89416/useless Del: bad code -- https://master.php.net/note/delete/89416/bad+code Del: spam -- https://master.php.net/note/delete/89416/spam Del: non-english -- https://master.php.net/note/delete/89416/non-english Del: in docs -- https://master.php.net/note/delete/89416/in+docs Del: other reasons-- https://master.php.net/note/delete/89416 Reject -- https://master.php.net/note/reject/89416 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151571) next »