note 125466 added to function.version-compare
| From: | jordyelfferich15 at gmail dot com | Date: | Mon, 26 Oct 2020 15:35:06 +0000 |
| Subject: | note 125466 added to function.version-compare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215653@lists.php.net to get a copy of this message | ||
I wrote a more reliable and straightforward function (as compared to other solutions posted here) to
compare versions. But does not support the operator argument like in the original.
<?php
function compareVersions($left, $right)
{
$leftParts = explode('.', $left);
$rightParts = explode('.', $right);
$leftCount = count($leftParts);
$rightCount = count($rightParts);
for ($i = 0; $i < max($leftCount, $rightCount); $i++) {
$leftPart = (int)($leftParts[$i] ?? 0);
$rightPart = (int)($rightParts[$i] ?? 0);
if ($leftPart !== $rightPart) {
return $leftPart > $rightPart ? 1 : -1;
}
}
return 0;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.174 (proxied: 62.163.90.162)
----
Manual Page -- http://php.net/manual/en/function.version-compare.php
Edit -- https://master.php.net/note/edit/125466
Del: integrated -- https://master.php.net/note/delete/125466/integrated
Del: useless -- https://master.php.net/note/delete/125466/useless
Del: bad code -- https://master.php.net/note/delete/125466/bad+code
Del: spam -- https://master.php.net/note/delete/125466/spam
Del: non-english -- https://master.php.net/note/delete/125466/non-english
Del: in docs -- https://master.php.net/note/delete/125466/in+docs
Del: other reasons-- https://master.php.net/note/delete/125466
Reject -- https://master.php.net/note/reject/125466
Search -- https://master.php.net/manage/user-notes.php