Add classes to compare versions easier than with compare_version()
and PHP_VERSION
.
composer require steevanb/version-comparator ^0.2
You can compare current PHP version with 2 versions to know if current php version is between this 2 versions:
PhpVersionComparator::isBetween('8', '9'); // return true is PHP is >= 8.0.0 and < 9.0.0
PhpVersionComparator::isBetween('8.0', '8.1'); // return true is PHP is >= 8.0.0 and < 8.1.0
PhpVersionComparator::isBetween('8.0.0', '8.0.2'); // return true is PHP is >= 8.0.0 and < 8.0.2
PhpVersionComparator
have a shortcut who call isBetween()
for each major and minor version:
// For PHP 5.3 to 5.6
PhpVersionComparator::isPhp5(); // return true is PHP is >= 5.0.0 and < 6.0.0
PhpVersionComparator::isPhp53(); // return true is PHP is >= 5.3.0 and < 5.4.0
PhpVersionComparator::isPhp54(); // return true is PHP is >= 5.4.0 and < 5.5.0
PhpVersionComparator::isPhp55(); // return true is PHP is >= 5.5.0 and < 5.6.0
PhpVersionComparator::isPhp56(); // return true is PHP is >= 5.6.0 and < 5.7.0
// For PHP 7.0 to 7.4
PhpVersionComparator::isPhp7(); // return true is PHP is >= 7.0.0 and < 8.0.0
PhpVersionComparator::isPhp70(); // return true is PHP is >= 7.0.0 and < 7.1.0
PhpVersionComparator::isPhp71(); // return true is PHP is >= 7.1.0 and < 7.2.0
PhpVersionComparator::isPhp72(); // return true is PHP is >= 7.2.0 and < 7.3.0
PhpVersionComparator::isPhp73(); // return true is PHP is >= 7.3.0 and < 7.4.0
PhpVersionComparator::isPhp74(); // return true is PHP is >= 7.4.0 and < 7.5.0
// For PHP 8.0 to latest 8.x
PhpVersionComparator::isPhp8(); // return true is PHP is >= 8.0.0 and < 9.0.0
PhpVersionComparator::isPhp80(); // return true is PHP is >= 8.0.0 and < 8.1.0
PhpVersionComparator::isPhp81(); // return true is PHP is >= 8.1.0 and < 8.2.0
If you want to throw an exception instead of returning the result,
each method have it's assert()
version who throw a VersionIsNotBetweenException
exception:
PhpVersionComparator::assertIsBetween('8', '9');
PhpVersionComparator::assertIsPhp5();
// etc