php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73102 Version compare is just broken.
Submitted: 2016-09-16 22:52 UTC Modified: 2016-09-17 09:52 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: greg dot chandler at wellsfargo dot com Assigned: cmb (profile)
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: greg dot chandler at wellsfargo dot com
New email:
PHP Version: OS:

 

 [2016-09-16 22:52 UTC] greg dot chandler at wellsfargo dot com
Description:
------------
The check should be >= on numeric, and = on alpha when checking.
When alphas are in the versions, the strings are not even compared.

Test script:
---------------
<?php

Function check_versions ($compare, $against)
  {
####  $VER_TO_COMPARE $VER_TO_COMPARE_AGAINST
    $bad_chars = array ( "-", "_", ".", "/", "(", ")" );
    $compare = strtolower(str_replace($bad_chars, ".", $compare));
    $against = strtolower(str_replace($bad_chars, ".", $against));
    if ( version_compare($compare, $against, '>=') )
      {
echo "Current: " . $compare . "         Against: " . $against . "     ";
        return "Pass";
      }
    else
      {
echo "Current: " . $compare . "         Against: " . $against . "     ";
        return "Fail";
      }
  }

echo check_versions("123456abc123de78z9y0", "123456abc123de78z9y0") . "\n";
echo check_versions("123456abc123de78z9y0", "123466abc123de78z9y0") . "\n";
echo check_versions("123466abc123de78z9y0", "123466abc123de78z9y0") . "\n";
echo check_versions("123466abc123de78z9y0", "123466abd123de78z9y0") . "\n";
echo check_versions("123466abd123de78z9y0", "123466abc123de78z9y0") . "\n";
?>






Expected result:
----------------
bash-4.1$ php /tmp/data/1.php
Current: 123456.abc.123.de.78.z.9.y.0         Against: 123456.abc.123.de.78.z.9.y.0     Pass
Current: 123456.abc.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Fail
Current: 123466.abc.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Pass
Current: 123466.abc.123.de.78.z.9.y.0         Against: 123466.abd.123.de.78.z.9.y.0     Fail
Current: 123466.abd.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Pass


Actual result:
--------------
bash-4.1$ php /tmp/data/1.php
Current: 123456.abc.123.de.78.z.9.y.0         Against: 123456.abc.123.de.78.z.9.y.0     Pass
Current: 123456.abc.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Fail
Current: 123466.abc.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Pass
Current: 123466.abc.123.de.78.z.9.y.0         Against: 123466.abd.123.de.78.z.9.y.0     Pass
Current: 123466.abd.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Pass


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-16 22:54 UTC] greg dot chandler at wellsfargo dot com
I do have sample code on how this is supposed to work if it's needed.
 [2016-09-16 23:02 UTC] greg dot chandler at wellsfargo dot com
Correction:


Expected result:
----------------
bash-4.1$ php /tmp/data/1.php
Current: 123456.abc.123.de.78.z.9.y.0         Against: 123456.abc.123.de.78.z.9.y.0     Pass
Current: 123456.abc.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Fail
Current: 123466.abc.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Pass
Current: 123466.abc.123.de.78.z.9.y.0         Against: 123466.abd.123.de.78.z.9.y.0     Fail
Current: 123466.abd.123.de.78.z.9.y.0         Against: 123466.abc.123.de.78.z.9.y.0     Fail
 [2016-09-17 09:52 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-09-17 09:52 UTC] cmb@php.net
> When alphas are in the versions, the strings are not even
> compared.

There are, however, no alphas involved in your test script. 'abc'
and 'abd' are treated as special version strings, and as neither
is recognized, they compare as equal, see
<http://php.net/manual/en/function.version-compare.php>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 14 12:01:32 2024 UTC