php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69268 version_compare returns wrongly when "special" characters are given
Submitted: 2015-03-20 09:35 UTC Modified: 2020-11-16 15:22 UTC
From: anlutro at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: Linux (Debian)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
12 + 31 = ?
Subscribe to this entry?

 
 [2015-03-20 09:35 UTC] anlutro at gmail dot com
Description:
------------
When doing version_compare on strings that contain "special" characters like parenthesis or brackets, it returns entirely the wrong result.

While this isn't an issue for 99% of version strings, one of our applications has the option of either providing a regular version string OR a datetime string with the git commit hash at the end (mostly for the developers' sake). We "solved" this by telling all developers submitting packages to our system to remove paranthesis or brackets from the git hash suffix.

This behaviour is consistent across all PHP versions: http://3v4l.org/DaQQY

Test script:
---------------
<?php
$v = '1.2.3 (59eda4e)';
var_dump(version_compare($v, $v));
var_dump(version_compare($v, $v, '<'));

$v = '1.2.3 [59eda4e]';
var_dump(version_compare($v, $v));
var_dump(version_compare($v, $v, '<'));

$v = '1.2.3 59eda4e';
var_dump(version_compare($v, $v));
var_dump(version_compare($v, $v, '<'));

Expected result:
----------------
int(0)
bool(false)
int(0)
bool(false)
int(0)
bool(false)

Actual result:
--------------
int(-1)
bool(true)
int(-1)
bool(true)
int(0)
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-16 15:22 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-11-16 15:22 UTC] cmb@php.net
<https://www.php.net/version_compare>:

| version_compare — Compares two "PHP-standardized" version number
| strings

"PHP-standardized" version numbers don't contain such "special"
characters.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 03:01:27 2024 UTC