It should be noted that version_compare() considers 1 < 1.0 < 1.0.0 etc. I'm guessing this is due to the left-to-right nature of the algorithm.
![]() |
|
||||||||||
|
version_compareDescriptionmixed version_compare ( string version1, string version2 [, string operator] )version_compare() compares two "PHP-standardized" version number strings. This is useful if you would like to write programs working only on some versions of PHP. version_compare() returns -1 if the first version is lower than the second, 0 if they are equal, and +1 if the second is lower. The function first replaces _, - and + with a dot . in the version strings and also inserts dots . before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. Then it splits the results like if you were using explode('.', $ver). Then it compares the parts starting from left to right. If a part contains special version strings these are handled in the following order: dev < alpha = a < beta = b < RC < pl. This way not only versions with different levels like '4.1' and '4.1.2' can be compared but also any PHP specific version containing development state.
If you specify the third optional
version_compare
Rickard Andersson
30-Oct-2007 02:18
It should be noted that version_compare() considers 1 < 1.0 < 1.0.0 etc. I'm guessing this is due to the left-to-right nature of the algorithm.
Jonathon dot Reinhart at gmail dot com
30-Oct-2007 08:38
I know this is somewhat incomplete, but it did a fair enough job for what I needed. I was writing some code that needed done immediately on a server that was to be upgraded some time in the future. Here is a quick replacement for version_compare (without the use of the operator argument). Feel free to add to this / complete it.
Hayley Watson
30-Jul-2007 10:31
Yes; I was incorrect. However "1.0pl" is greater than "1.0.1", since "pl" comes after any number (this should perhaps be noted in the documentation: dev < alpha = a < beta = b < RC < # < pl)
opendb at iamvegan dot net
11-Jun-2007 01:26
My concern at this point, is that Hayley Watson's comments may confuse some people - it did me, so I did some more investigation before replying...
Hayley Watson
10-Jun-2007 06:47
To correct opendb at iamvegan dot net's misapprehension: "1.0pl1" is regarded as less than "1.0.1". But the call as you've written it asks whether "1.0pl1" is greater (">") than "1.0.1" - which it isn't.
opendb at iamvegan dot net
10-Jun-2007 05:01
Something that may trip some folks up, but is useful to mention is that the following version comparison does not work quite as I expected:
MagicalTux at ookoo dot org
23-Oct-2006 06:38
To answer elmuerte's note (06-Jul-2006 03:24), you'd even better remove spaces than replacing them.
arnoud at procurios dot nl
29-Sep-2004 02:28
If you're careful, this function actualy works quite nicely for comparing version numbers from programs other than PHP itself. I've used it to compare MySQL version numbers. The only issue is that version_compare doesn't recognize the 'gamma' addition that mysql uses as being later than 'alpha' or 'beta', because the latter two are treated specially. If you keep this in mind though, you should have no problems.
mina86 at tlen dot pl
30-Jun-2004 07:40
Here's a wrapper which is more tolerant as far as order of arguments is considered:
aidan at php dot net
26-Jun-2004 03:02
This functionality is now implemented in the PEAR package PHP_Compat.
eric at themepark dot com
21-Jun-2004 09:50
[editors note]
sam at wyvern dot non-spammers-remove dot com dot au
23-May-2004 11:18
Actually, it works to any degree: |