Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP version compatibility check

Tags:

php

How do I know my code which version of the language is compatible? Is there a way to figure out which is the minimum version of PHP language that can execute it without error? Maybe there is some Checker or a function, method, class?

like image 994
Българският Войн Avatar asked Jul 18 '15 10:07

Българският Войн


People also ask

What is PHP compatibility?

PHPCompatibility is a set of sniffs for PHP_CodeSniffer that checks for PHP version compatibility. It will allow you to analyse your code for compatibility with higher and lower versions of PHP.

Is PHP 8 backwards compatible?

Is PHP 8 backwards compatible ? A: PHP 8 new Features also introduces us to unions in PHP 8. You can learn more about this in PHP benchmarks. This means that there is an issue with backwards compatibility if you implement PHP 8.0 union types, as this will break on sites running PHP 7.4 or below.


3 Answers

I used the PHPCompatibility/PHPCompatibility library to check my project's version compatibility, I recommend.

You can specify the version of PHP you want to check. For example, to test PHP 7.3:

./vendor/bin/phpcs -p . --standard=PHPCompatibility --runtime-set testVersion 7.3
like image 89
Domingos Coelho Avatar answered Oct 23 '22 05:10

Domingos Coelho


Install the PHP version you want to test and run php -l file.php to test if the file passes the lint check. If PHP is unable to parse the file, it will tell you.

In general, you should be aware of which features became available in which version. Disregarding PHP 5.3 with its several patch versions that added significant improves for a moment, this boils down to knowing what features were brought with PHP 5.4, 5.5, 5.6 and 7.0, and explicitly pointing to that version in your composer.json.

As a hint: 5.4 has short array syntax and traits, 5.5 has generators and finally, 5.6 comes with variadic functions and argument unpacking, and 7.0 has scalar type hinting and return types. It helps to use a IDE that knows about these features and warns you if you use something that isn't supported in the version you selected.

PHP comes with a constant PHP_VERSION that contains the current version you are running on, and has a function version_compare() to allow for easy comparing of version notation as in "which one is greater". This would allow to execute different parts of the code depending on the version, allowing to add compatibility layers for some things you need if you run on lower versions, and using the PHP implementation (usually faster) when running on more recent versions.

Apart from this, you will always stumble upon problems with extensions not being installed. This isn't a problem with the PHP version itself. PHP has function_exists() and method_exists() to detect if you can call something before you do (and fail with a fatal error). You can detect this error condition and either have a different solution, or inform the user he has to add something to his PHP installation.

I would recommend using Travis CI for open source projects. Basically you get it for free, and adding different PHP versions is like adding a new line in the travis.yml configuration file. They do offer plans for private repositories as well. Any other CI installation offering you plenty of PHP versions will also work as long as you are running your code on all the PHP versions you intend to support.

Final suggestion: Drop support for PHP 5.3 and 5.4. These versions are out of maintenance (or leaving security-only fix phase in 2 months from now) and shouldn't really be targeted anymore.

like image 39
Sven Avatar answered Oct 23 '22 03:10

Sven


https://3v4l.org/

This online tool shows your code output for more than 150 different PHP versions (every version released since 4.3.0) plus HHVM.

Not sure if it's enough for your purposes.

like image 34
evgpisarchik Avatar answered Oct 23 '22 03:10

evgpisarchik