Doc #73102 [Opn->Nab]: Version compare is just broken.

From: Date: Sat, 17 Sep 2016 09:52:47 +0000
Subject: Doc #73102 [Opn->Nab]: Version compare is just broken.
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-13915@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73102&edit=1 ID: 73102 Updated by: cmb@php.net Reported by: greg dot chandler at wellsfargo dot com Summary: Version compare is just broken. -Status: Open +Status: Not a bug Type: Documentation Problem Package: Unknown/Other Function Operating System: Linux PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: > 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>. Previous Comments: ------------------------------------------------------------------------ [2016-09-16 23:02:29] 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-16 22:54:08] 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 22:52:53] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73102&edit=1

« previous php.doc.bugs (#13915) next »