Test Code on Older PHP Without Having to Downgrade

The purpose of this article to be an eye-opener for you – there is no need to torture yourself with installing old PHP versions. Instead, use 3v4l.org to test smaller pieces of your code against every PHP version there is. I’ll give you some real-life examples that saved me from releasing software that only runs on the latest PHP. While I agree that developers should be looking forward and use new paradigms and syntax, people still try to use new code in an old environment. Fatal errors are commonplace, yet easily avoided with a little care. This article tells the easier half of our PHP versions story, while the other will show you how to determine the minimum PHP version for your entire project.

Why is it so hard and bad to install old PHP?

If you use Linux or manage your server (dedicated, VPS, etc.), you might notice that older, unsupported PHP versions are disappearing from package managers, repositories, and Phpbrew. As you can see, as new versions are released, old ones become unavailable:

Phpbrew is not the best tool to test code on old PHP

The ppa:ondrej/php repo greets you with this message:

The Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP for Supported Ubuntu Releases are provided. Don’t ask for end-of-life PHP versions or Ubuntu release, they won’t be provided.

And in general, maintaining different PHP versions along with the required extensions can be a pain. It’s certainly possible as we keep both the latest PHP 5 and the newest PHP 7 and can switch between them easily. However, that’s not the scope of this article.

3v4l.org to test your code on older PHP

The 3v4al.org site (leetspeak for eval) is priceless for us that develop in PHP. Just paste a few lines of code about which you are not sure. Whenever there is a new syntax or a function from the future, the eval() button will tell you exactly how your code will behave on older PHP installations. It tests the snippet on every single PHP release and presents you the version groups where the output or errors are different. Yes, it shows the errors your code generates, which might differ between versions.

Example #1: Short array syntax

It’s a cute little mistake. A developer might get distracted and instantiate an array like how they do it in JS:

var a = [];

But if you think you can get away with this in PHP:

$a = [];

You’d be wrong. It’ll probably work everywhere you test because us developers like to keep up with PHP versions and use a decent server. But a customer of our WP plugins might not.

As you can see on DisplayWP, the minimum required PHP version of WordPress 5 is still 5.2.4 – which has been around for well over a decade. Yes, plugins can set different minimum requirements, but still, it’s of some concern!

Example #1 is a seemingly benign misstep. At first, you might not even know what the name of this syntax is or how to look it up in the docs. After some digging, here is some official information:

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

When in doubt, it’s often faster and easier to put it through 3v4l.org to test the code on many older PHP versions and get reassurance. It immediately tells you that it’s OK since PHP 5.4, but any code like the above will fail miserably with a Parse error: syntax error, unexpected '[' ...

Example #2: Type declaration or type hinting of function arguments

Declaring types can make your PHP a bit stricter. This feature of PHP keeps improving with new possible options. It’s something that has an unchanged syntax and has been working since PHP 5. However, what you can hint is dependent on the version. Therefore, it can happen that you accidentally declare something that only got introduced in PHP 7. It’s like once you start using hints in the code, something switches in your mind and suddenly you want to use declaring for every type there is. Unfortunately, that’s not possible if you have users with old PHP.

function expect_an_exception(Exception $e){}
function expect_a_string(string $string){}
expect_an_exception(new Exception('Division by zero.'));
expect_a_string("hello");

Once you test this piece of code on old PHP with 3v4l.org, you can see that since PHP 5 you can use class names for hinting. Isn’t it weird that it came first, not the native language types such as bool, string, int?

Example #2 also shows that declaring your expectation for a string is actually a PHP 7 feature, and it just doesn’t work in older PHP. You get an easter-egg-like error that your customers won’t find all that funny:

Catchable fatal error: Argument 1 passed to expect_a_string() must be an instance of string, string given in...

What’s more confusing is that you can use array hint since PHP 5.1. It’s chaos if you ask me. I admit I had a hard time keeping these in mind. The useful 3v4l.org helped me numerous times.

PHPStorm as an alternative?

I wanted to present a free tool/service to test your code snippets on past PHP versions. Of course, I’m aware that other solutions exist. Many of us don’t have fancy, paid IDEs like PHPStorm. Yet Attila from our team uses it daily. Those IDEs can probably tell you that there is a problem with your code on the chosen PHP version. But what if you code by hand in Sublime Text or on a similar, more bare editor? I code on the (now-AWS) Cloud9 and while it’s fancy it doesn’t have many features for PHP, so I’m left with augmenting my IDE with a few external tools like this. I don’t mind.

Do you think 3v4l.org will help you too? Share it in the comments!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Our Newsletter!

Get secret tips and valuable details about making your site more successful.

Follow Us!

We Recommend

This site is powered by Elementor

100% Free  WP Website Builder  All-in-One Solution

Monthly Archives

Scroll Up