So, you might already know that PHP 7 is already available and one of its main promises is that it’s going to give a big performance boost to your apps. There are other advantages that come with PHP 7 and we’ve listed the most relevant ones here (there are a few blog posts that already explain them):

New Error Handling

One of the noticeable improvements in PHP 7 is the feature in the new Exception Engine. With this new engine, any fatal error from the previous PHP version will generate an exception that the developer can catch. This allows for a better control over any possible errors.

Scalar Types and Return Types

Functions now can have parameters with a type as well as return the type. This allows the developer to define the types of the functions and will get an error if he sends a wrong type. These types of casting errors will generate an exception within the new Exception Engine, so it’s always possible to recover from these errors.

Spaceship operator

The new <=> operator will allow you to compose comparisons using only one operator.

This expression will evaluate to -1 if $a is smaller than $b, 0 if $a equals $b, and 1 if $a is greater than $b.

You can check the official PHP 7 new features page to see all of them. There’s also a Github repository that list all the features included. You might be wondering if all these promises are true or simply hype, and if your projects are going to work on the new PHP 7 version.

So at Mobile Jazz, we decided to do a test using one of our projects to see if PHP 7 works on our current code. Our objective was to see if we encounter any performance improvement as promised.

How to install PHP7

Naturally, the first step in testing PHP 7 is to install it. Right from the get-go, we already stumbled on our first problem. We found that if your servers are using Ubuntu 14 (which most of our servers are using), there’s no official apt package to install it, therefore you might need to use 3rd party repositories. If you are already using Ubuntu 16, then you’re in luck and you have PHP 7 ready to be installed in your server.

For our test, we decided to upgrade one of our Ubuntu 14.04 servers to Ubuntu 16. We followed the Digital Ocean guide to upgrade our server. Here’s a preview of the guide we followed when upgrade our server.

Update your system

sudo apt-get update
sudo apt-get dist-upgrade

Install Ubuntu upgrade manager

sudo apt-get install update-manager-core

Run the upgrade

sudo do-release-upgrade -d

The complete installation was successful, but we encountered a problem with the MySQL server. As it was a test server, we decided to remove the /etc/mysql/my.cnf file and updated the packages again. After the Ubuntu upgrade, we were ready to try our site using the new PHP 7 engine. Overall, we encountered some issues, but nothing major. The following is a list of the problems we experienced:

1. After the upgrade, some of the PHP modules we had already installed on our server were missing, so we added it manually:

sudo apt-get install php-mbstring php-curl

2. Some of the errors from PHP 5 had been moved to PHP 7 warnings, so our site was showing errors that we didn’t see in the previous version. It was clearly our mistake, but we found it strange they didn’t show up in previous versions of PHP. This was the main issue that popped up:

Declaration of ... should be compatible with …

3. Another problem we encountered, which might have been caused by Yii2, is that when you request a URL with a GET method that had an empty body and set ‘content-type = application/json’ in the header, you will get an error message. That’s something that worked perfectly on previous PHP versions.

Performance Boost

PHP 7 claims to be a lot faster than PHP 5. In fact, they claim that PHP 7 is 2x faster than the previous version, so we decided to give it a try on a real-world example to see if there’s any truth to this.

For this test, we used a website from one of our clients, and tested it using both PHP5 and PHP 7. The code of the website we used had not been optimized yet as it’s a website we have just launched and still in the process of optimizing.

We decided to create two droplets on Digital Ocean using the same configuration, but one with PHP 5 and the other one with PHP 7. We haven’t done any performance tuning of the servers. All of the configurations are using the default values on the Ubuntu installation.

First, we ran a stress test on the website, simulating a user accessing it and performing a variety of actions. Essentially, we simulated the actions of a typical user, such as logging in to the website and visiting different pages. The website was written using Yii2 and a MySQL Server, so every visit to a webpage is running a lot of PHP code behind it. Here are the results of our test:

PHP 5
PHP5-tests-1

PHP 7
PHP7-tests-1

We simulated 50 users visiting the website for an average duration of 10 seconds. As you can see from the results, the claim that PHP 7 would have twice the speed of the previous versions is not entirely correct. While there is a small performance improvement, it’s only around 10%-20% increase.

We conducted another test using an API. This test used the same Yii2 application and mainly runs a query to the database, and then encodes the data to JSON. The results of this test are the following:

PHP 5
PHP5-tests-2

PHP 7
PHP7-tests-2

In this case, the performance difference is not as substantive as PHP claims.

Conclusion

It took a long time to get a new version of PHP and it is time to start moving to the new version, but don’t expect big performance improvements. If you want to optimize your site performance, you would still need to use the same techniques as before and don’t hope PHP 7 is going to save you from headaches during the optimization process. Having said that, PHP 7 shows it’s heading in the right direction. Now, all PHP has to do is make the improvements exponential rather than taking incremental steps.

At Mobile Jazz, we believe in ideas and we will help you develop them. We strive to be at the top of what’s happening in technology, digital marketing and design. We continuously educate ourselves, and experiment with the latest trends to stay ahead of everyone else. That’s why we’re able to give you a competitive advantage in the market. Simply drop us a line and we’ll take your project one step ahead!

Jordi Giménez

Jordi Giménez

Jordi has worked as a project manager, developer and security analyst in web, iOS and Android. He’s worked for companies big and the small in government, banking, insurance, healthcare and IT.