PHP 8.1: version_compare operator restrictions


In PHP 8.1, the version_compare() function is made more strict with the operator parameter.

In PHP 8.0, this was made to throw a ValueError exception if an unknown parameter was passed, as part of the internal function exception handling changes.

In PHP 8.1, this is further restricted to disallow partial values for operator parameter.

version_compare(1, 2, '');
version_compare(1, 2, '!');
version_compare(1, 2, 'g');
version_compare(1, 2, 'l');
version_compare(1, 2, 'n');

All of the statements above are were allowed in PHP 8.0, and are no longer allowed in PHP 8.1. These values are undocumented values from the documentation.

Note that PHP 8.0 started to throw TypeError for all invalid values. The operators disallowed in PHP 8.1 are due to internal partial matching changes.

In PHP 8.1, only the following operators are allowed:

  • ==, =, and eq
  • !=, <>, and ne
  • > and gt
  • >= and ge
  • < and lt
  • <= and le

Backwards Compatibility Impact

PHP 8.0 started to disallow a vast majority of disallowed values, and this change in PHP 8.1 is a continuation of it. The disallowed values were not documented.

Using the correct values fixes the issue for all PHP versions.