What does version compare?

Asked

Viewed 84 times

2

I was reading about version_compare() of PHP.

Still, I didn’t understand much of the subject, could you explain a little more about?

1 answer

4


Definition

version_compare() compares two versions, represented by strings, that are/are standardized by PHP.

version_compare($versao1 , $versao2);

Parameters

The parameters to use the version_compare() are quite simple:

  • version 1 - First version number.
  • version 2 - Second version number.
  • operator (optional) - When specified a test will be performed for a specific relationship.

if (version_compare(phpversion(), "4.3.0", ">=")) { // versão do PHP maior que 4.3.0 } else { // versão do PHP menor que 4.3.0 }

Operators that can be used are: <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne.

Results

When the operator is not used:

  • -1 if the first version is less than the second, 0 if they are equal and 1 if the second version is less than the first version.

When using the operator:

  • TRUE if the comparison is as specified by the operator, FALSE otherwise.

With standardized by PHP, I mean the fact that both must demonstrate the same format that is approved by PHP, for example:

version_compare('5.2', '5.2.0'); // -1

Will return -1, for the 5.2.0 is not standardized in comparison.

However the same comparison, done better returns the expected:

version_compare('5.2.1', '5.2.0'); // 1
version_compare('5.2.0', '5.2.0'); // 0
version_compare('5.2.0', '5.2.1'); // -1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.