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
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: anlutro at gmail dot com
New email:
PHP Version: OS:

 

 [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 26 03:01:32 2024 UTC