21

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?

3
  • 5
    There could also be a maximum version of PHP language that can execute it without error. Commented Jul 18, 2015 at 10:33
  • 1
    Ok but how to know his compatible? Commented Jul 18, 2015 at 10:36
  • Either you know your code well and you'll check each functions docu regarding PHP version or you simply run a syntax check on several servers with the versions you want to ensure compatibility Commented Jul 21, 2015 at 14:22

6 Answers 6

17

Most likely you need to have different PHP versions installed. Then you can check compatibility of your code with specified PHP version using shell:

find . -name *.php | xargs -n1 /usr/bin/php -l

php -l command runs PHP in syntax check only mode. The command above will check each PHP file in your project againts compatibility with PHP version located at /usr/bin/php.

Sign up to request clarification or add additional context in comments.

4 Comments

I use localhost now. Is there any software to check whether my code is compatible with any version?
Or you can use a single command: find . -type f -iname '*.php' -exec /usr/bin/php -l {} \;
throws unknown primary or operator find . -name '*.php' | xargs -n1 /usr/bin/php -l works on mac
Unfortunately it does not detect removed functions so it is of little use.
15

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.

2 Comments

I have written classes now. Is there software that can scan them and say you are compatible or not at the localhost?
As I said: Install PHP in the version you want to check and run the php -l command to see if PHP can parse it.
13

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

1 Comment

The latest release of PHPCompatibility 9.3.5 was released on Dec 27, 2019 and does not detect php 8 code compatibility.
5

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.

Comments

2

Here's how to test your code for php 8.0 compatibility (slightly improved on https://odan.github.io/2020/12/22/php8-compatibility-check.html):

You cannot install PHPCompatibility from the release branch because the latest version was released before php 8.0 and is not compatible with it.

# install from development branch
composer require --dev squizlabs/php_codesniffer phpcompatibility/php-compatibility:dev-develop

# colorized output
phpcs --config-set colors 1

# test code in src dir
vendor/bin/phpcs src -p --report=code --ignore=vendor/* --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.0

Another way is to enable the logging of everything deprecated by setting E_DEPRECATED in the php.ini config.

; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
error_reporting = E_ALL

3 Comments

The latest release of PHPCompatibility is from 2019. It's not useful right now.
See also opencollective.com/php_codesniffer/updates/…, to see what the dev has been working on.
1

The Docker provides the ability to follow a suggested by pamelus proposal with a really small efforts:

docker run --rm -ti -v /path/to/your/app/src:/app php:8.0 bash
find /app -name '*.php' | xargs -n1 /usr/local/bin/php -l > /app/reportphp8

So php:8.0 is a tag which can be obtained from DockerHub page of PHP. By changing this version your can easily check your app's compatibility with 8.1/9.3/whatever version you want.

/path/to/your/app/src is a directory of your application sources root (the directory containing source code of YOUR application only, i.e. src/ dir in Symfony and app/ in Laravel. That's done to prevent checking syntax of the vendors dir).

The report will be stored in your application root named reportphp8.

Important

The advice above is about the syntax only, i.e. it can detect whether you use removed or deprecated language structures or core functions. It won't be able to detect if you supply the correct types to functions having changed signature, etc. For those purposes use static analyzers like Pslam/PHPStan/Phan.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.