|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-12-10 22:20 UTC] john at zerocrates dot org
Description: ------------ The documentation (just recently updated to include the information about version_compare returning null on an invalid comparison operator in prior versions, from doc bug #77838), doesn't mention the new behavior of the function in 8.0.0: it throws ValueError instead. This also should possibly be mentioned as an incompatible change in the migration to 8.0.0 document. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 12:00:01 2025 UTC |
The treatment of the empty string is a new one to me: I would say it's a bug, but probably a long-preexisting one. The checks for the comparison operations for version_compare all take this basic form: if (!strncmp(op, "<", op_len) || !strncmp(op, "lt", op_len)) { repeated for each set of equivalent operations and abbreviations. op_len is the length of the passed operation string, so when the empty string is passed, op_len is zero and strncmp will always consider its arguments to be "matching" and return 0. So empty string is treated as a less-than operation simply because less-than is the first check. This structure of the checks also means that "even more abbreviated" forms of the multi-character operations are also accepted: "l" will be treated as "lt", "g" as "gt", "e" as "eq" and both "n" and "!" as "ne"/"!=". Is this what you were referring to in your comment about "abbreviations"? I'd say those were probably not intended either (for example, the code explicitly checking both "=" and "==" would seem to indicate that this behavior wasn't expected, plus the "feature" not being documented), but they've probably been there untouched and working for something on the order of two decades. so I would understand that simply documenting their presence might be preferable. The empty string handling is probably just about (if not exactly) as old. PHP 8 itself, as far as I can tell, made no changes to this behavior, buggy or not, beyond simply converting the null return to a ValueError.