php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25811 version_compare fails on some versions
Submitted: 2003-10-09 16:08 UTC Modified: 2003-10-13 02:58 UTC
From: pmateescu at novosadhayes dot com Assigned:
Status: Not a bug Package: PHP options/info functions
PHP Version: 4.3.2 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pmateescu at novosadhayes dot com
New email:
PHP Version: OS:

 

 [2003-10-09 16:08 UTC] pmateescu at novosadhayes dot com
Description:
------------
version_compare seems to return invalid results in two cases:
0.1a vs 0.1 and 1.0.a vs 1.0. in both cases the value on the left should be the newest version.
If the a (alpha) is confusing it, then it does the same for other letters (e.g. M).

Reproduce code:
---------------
$test = array (
	array('0.1', '0.1', 0),
	array('0.1', '0.2', -1),
	array('0.3', '0.2', 1),
	array('0.1.0', '0.1', 1),
	array('0.1a', '0.1', 1),
	array('0.1m', '0.1', 1),
	array('0.1z', '0.1', 1),
	array('0.1a', '0.1b', -1),
	array('0.1.a', '0.1', 1),
	array('0.1.a', '0.1.b', -1),
	array('0.1-pre2', '0.1-pre1', 1),
	array('0.11', '0.2', 1),
	array('0.11.1', '0.2.3', 1)
	);

foreach($test as $vals) {
	$ret = version_compare($vals[0], $vals[1]);
	echo "$vals[0] vs $vals[1] should be $vals[2] and is $ret ...[". ($ret === $vals[2] ? 'PASSED' : 'FAILED') ."]\n";
}


Expected result:
----------------
all of them should return PASSED.

Actual result:
--------------
0.1 vs 0.1 should be 0 and is 0 ...[PASSED]
0.1 vs 0.2 should be -1 and is -1 ...[PASSED]
0.3 vs 0.2 should be 1 and is 1 ...[PASSED]
0.1.0 vs 0.1 should be 1 and is 1 ...[PASSED]
0.1a vs 0.1 should be 1 and is -1 ...[FAILED]
0.1m vs 0.1 should be 1 and is -1 ...[FAILED]
0.1z vs 0.1 should be 1 and is -1 ...[FAILED]
0.1a vs 0.1b should be -1 and is -1 ...[PASSED]
0.1.a vs 0.1 should be 1 and is -1 ...[FAILED]
0.1.a vs 0.1.b should be -1 and is -1 ...[PASSED]
0.1-pre2 vs 0.1-pre1 should be 1 and is 1 ...[PASSED]
0.11 vs 0.2 should be 1 and is 1 ...[PASSED]
0.11.1 vs 0.2.3 should be 1 and is 1 ...[PASSED]

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-13 02:58 UTC] sniper@php.net
RTFM: version_compare --  Compares two "PHP-standardized" version number strings. 

Notice the "PHP-standardized" there.
There is no bug here, 0.1 > 0.1x where x == a-z

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 05:01:27 2024 UTC