newdjango
newdjango

Reputation: 31

Compare two version numbers with 4 decimal points in PHP

How can i compare two numbers in PHP which have four decimal places. version_compare does not seem to work with four decimal places. It will work for three.

Example: Is a > b Compare: a : 16.8.1.22.23 b : 16.8.1.23.40

Is there an easy way to do this ?

Upvotes: 1

Views: 342

Answers (1)

David Ansermot
David Ansermot

Reputation: 6112

You can use the method

mixed version_compare ( string $version1 , string $version2 [, string $operator ] )

As third argument you set the comparison operator : <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne

For exemple :

if (version_compare('16.8.1.22.23', '16.8.1.23.40', '>')) {
   // do something
} 

In this exemple it won't enter the if because $version1 is lesser than $version2.

ref : reference documentation for version_compare

Upvotes: 2

Related Questions