What does version compare?

2

I was reading about PHP's version_compare() .

Still, I did not understand much of the subject, could you explain a little more about?

    
asked by anonymous 04.08.2017 / 21:39

1 answer

4

Definition

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

version_compare($versao1 , $versao2);

Parameters

The parameters for using 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 it is the same, and 1 if the second is smaller than the first.

When you are using the operator:

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

With standard PHP, I mean to the fact that both should demonstrate the same format that is approved by PHP, for example:

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

It will return -1 , since 5.2.0 is not standardized in the 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
    
04.08.2017 / 21:41