php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77838 version_compare() returns null when invalid operator given
Submitted: 2019-04-03 14:46 UTC Modified: 2019-04-03 17:22 UTC
From: ravage at bluewin dot ch Assigned: cmb (profile)
Status: Closed Package: PHP options/info functions
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ravage at bluewin dot ch
New email:
PHP Version: OS:

 

 [2019-04-03 14:46 UTC] ravage at bluewin dot ch
Description:
------------
According to the documentation of the version_compare() function, it returns either true or false, if there is an operator parameter given.

https://www.php.net/manual/en/function.version-compare.php

But it seems it can also return null, if there is an invalid operator string given.
Invalid can be at least either a totally wrong operator, e.g. "invalid", a space " " or a valid operator string pre- or suffixed with a space, e.g " ge".

This is certainly a documentation issue but for the future this could be improved by throwing an exception, e.g. InvalidArgumentException.

By the way this was reported as a comment on the function's documentation page at https://www.php.net/manual/en/function.version-compare.php#106715

Test script:
---------------
<?php
var_dump(version_compare('3.7.5', '3.7.0', 'ge')); // true, OK
var_dump(version_compare('3.7.5', '3.7.0', ' ')); // null, should throw invalid argument exception at best
var_dump(version_compare('3.7.5', '3.7.0', ' ge')); // null, should throw invalid argument exception at best
var_dump(version_compare('3.7.5', '3.7.0', 'ge ')); // null, should throw invalid argument exception at best
var_dump(version_compare('3.7.5', '3.7.0', 'invalid')); // null, should throw invalid argument exception at best
?>

https://3v4l.org/CrO0J

Expected result:
----------------
That the possible return value of null is officially documented at https://www.php.net/manual/en/function.version-compare.php.
Even better would be an exception thrown, e.g. InvalidArgumentException.

Actual result:
--------------
null (officially undocumented)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-03 17:21 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=347125
Log: Fix #77838: version_compare() returns null when invalid operator given
 [2019-04-03 17:22 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2019-04-03 17:22 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2019-04-03 17:25 UTC] salathe@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=fbb9a28e62178d7374b37c16cd6f83b7dd8fd9a2
Log: Fix #77838: version_compare() returns null when invalid operator given
 [2020-02-07 06:05 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=442996b447e3d77d5cda9a50da78007e739a1d3f
Log: Fix #77838: version_compare() returns null when invalid operator given
 [2023-11-17 13:52 UTC] bernie dot durfee at renaissance dot com
The docs still don't reflect that the return value that could be null.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 17:01:30 2024 UTC