I was reading about PHP's version_compare()
.
Still, I did not understand much of the subject, could you explain a little more about?
I was reading about PHP's version_compare()
.
Still, I did not understand much of the subject, could you explain a little more about?
version_compare()
compares two versions, represented by strings , which are / are standardized by PHP.
version_compare($versao1 , $versao2);
The parameters for using version_compare()
are quite simple:
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
.
When the operator is not used:
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