PHP7 – What’s New and Better in PHP?

In some of my previous posts, I discussed about using PHP as part of LAMP for web application development. Well, if you haven’t noticed it already, when I said PHP, I was talking about PHP version 5 or 5.x. However, now I’m going to specifically talk about PHP7 and what new features it brings to the table.

PHP7 Logo
PHP7 Logo

PHP History

PHP5, which is the most popular and most widely used PHP version, was introduced in 2004 and immediately after its launch, development of PHP6 was started. The main goal of proposed PHP6 was to add native support for Unicode characters so that it could allow multiple languages. However, despite years of development effort, native Unicode support couldn’t be achieved and the release of PHP6 got postponed. There had been other developments in PHP other than Unicode support, which were then released in minor releases of PHP5 as 5.3 and 5.4.

After 11 years of the launch of PHP5, the community finally decided to publish its first major release. Since one of the main goals of PHP6 (native Unicode support) wasn’t completed and thus abandoned in 2010, most of the members voted to skip the release of PHP6 to avoid confusion and to directly launch PHP7 (December 3, 2015). As a matter of fact, PHP7 also doesn’t provide native support for unicode, but supporting unicode wasn’t the top priority of this version from the start, so it got launched as the first major release of PHP after 11 years of development.

PHP7 Advantages

It is common to have high expectations from something that has been awaited for so long and PHP7 has rightly justified most of those expectations as well. The major plus point comes with Performance, which has been improved by hopping 100% i.e. PHP7 is twice as fast as PHP5. What this means is PHP7 can handle double the amount of requests than the older version with the same amount of computation resources. So, with this, we need about half as much servers as we need with PHP5. This provides a huge cost saving and performance boost to the industry. Also, some of the widely used PHP based frameworks like WordPress, Drupal, Zend, Laravel, and so on are also going to perform better.

PHP7 Defined

Above, I told you that PHP7 provides better performance than its predecessors and you might be wondering how did it achieve this performance boost. Before I answer this, here’s what you need to understand first: PHP is an interpreted programming language. I won’t go into detail about Interpreted and Compiled programming languages. But I certainly recommend you to research about them on your own, as their knowledge is truly important. Briefly speaking, although interpreted languages are generally fast to code, test and execute, they also have some cons. The major drawback is the lack of speed compared to compiled languages.

Probably, you now might be able to guess what PHP7 did to have better performance. If you guessed it to have something to do with code compilation, rather than the older directly interpreted style, then you guessed it right. Yes, it’s optimized to process your PHP code and translate them into Assembly language before execution. This means it moves lot less information around memory, thus reducing its memory usage significantly. In addition, it’s also developed in a way to provide consistent support for 64-bit architecture. This is the optimization which led to 2x growth in performance.

Comparison with HipHop Virtual Machine

If you’ve been keeping yourself updated with the latest development in this field, you might have heard about HipHop Virtual Machine (HHVM). HHVM was developed by Facebook to optimize the performance of their PHP application. Basically, what it does is to compile the PHP code into C++ compiled code, which is much faster than interpreted language execution. And with the launch of version 7, PHP is now as competitive as HHVM in terms of performance. In fact, some of the test cases found it to be even more faster than HHVM. But there are other test cases also which found HHVM to be faster. So, let’s not decide on which one is better, and let’s just be glad that PHP is as fast as HHVM, right?

Upgrading to PHP7

If you’re currently using PHP5 and wish to upgrade to version 7, which is highly recommended, you can simply follow various upgrade guides available in the Internet. Since I did a blog post on setting up LAMP in one of my previous posts, I’m just going to upgrade that system here.

Once you’re inside your CentOS machine as root user or any sudoer, you can enter following commands to upgrade your PHP system to PHP7:

[code]
[root@lamp ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@lamp ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@lamp ~]# yum install yum-plugin-replace
[root@lamp ~]# yum replace php-common –replace-with=php70w-common
[/code]

UPDATE: If you've got an existing PHP5 application, don't just go ahead and upgrade your PHP version. Since there are numerous changes, some of which are incompatible with earlier versions, unplanned upgrade may result in fatal errors in your application. Just to mention some incompatible changes; mysql_* functions have been replaced by PDO class for database connection and queries. That means, your application won't be able to connect with database if upgrade is done without necessary amendments in code. Also, there are some significant changes in Error handling, so you better be very careful before migration.

Well, in this way, I got my web server upgraded to have benefits of PHP7. Along with performance, PHP7 also introduces lots of new features and changes from syntax perspective. Since covering all of them isn’t viable in this single post, I guess we can talk about them later. I hope this post has been informative and useful for you. Please let me know of your opinion and feedback in the Comments section below. Thank you for reading!


Comments

Leave a Reply

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