It has been almost a year since it became clear that PHP 5.7 had all chances to become PHP 7 – the new stable version of the PHP programming language.
After 8 release candidates and a few release date delays, the new stable player on the PHP ground is now officially out and fully enabled on our web hosting platform as well.
This release marks the start of the new major PHP 7.x series, which is a milestone event on the programming horizon.
The story behind PHP 7 in short
When PHP 6 was released back in 2005, it came forward in public space as the logic future descendant of the PHP 5.x series.
By offering an alternative encoding approach where Unicode is brought into the PHP core engine, PHP 6 promised to change the coding landscape for good.
However, that approach could not stand the real-world tests due to the excessive use of CPU and memory resources, which made PHP 6 too complex and resource-consuming to put into practice.
It seemed like the PHP 5.x branch would have to release a candidate to lead the ‘new stable PHP version’ march. It was time to focus on internal optimizations, which gave rise to the experimental PHPNG (also known as PHP 5.7).
It took the best of both worlds by assimilating the most worthy features of PHP 6, among them namespaces support, and the performance and memory utilization improvements initiated in the PHP 5.x series.
This is how PHP 7’s path was paved. Since the development community chose to take the new stable candidate out of the PHP 5.x branch stage, it was a logical step to move straight to PHP 7, skipping PHP 6.
What are the main advantages of PHP 7?
PHP 7’s concept revolves around ‘spaceship-like’ performance characteristics, among them faster speeds and less memory consumption.
Also, it brings new operators and a range of type declarations to the table. Here are the main advantages in detail:
The most striking advantage of upgrading to PHP 7 is the practically doubled real-world performance speeds.
Behind this amazing improvement lies the brand new version of the Zend engine that has been implemented under the code name of PHP#NG.
The refactored Zend Engine offers significantly reduced memory usage and a newly implemented just-in-time compilation (JIT) functionality, which resolves the previous prior-to-execution compilation problems.
On the figure below, you can check the performance benchmark figures provided by the Zend Performance Team and see for yourself:
As you can see, the performance gains realized by PHP 7 vary between 25% and 70% as far as real-world apps are concerned.
You can see the full infographic on PHP 7 performance on the Zend website.
Apart from the doubled speeds, PHP 7 also gives developers the freedom to enhance the quality of their code through accurate return type declarations.
Developers can now prevent unintended return values by declaring what kind of return type a function is expected to have.
To enhance the feature even further, PHP 7 brings in 4 new type declarations for scalar types, allowing developers to denote that they’re expecting strings, floats, ints or booleans to be returned.
The new scalar type declarations are also supported by the existing argument Type Hints.
PHP 7 has introduced a new operator – the Combined Comparison Operator, which is more popular as the Spaceship Operator <=>.
It is similar to a TIE fighter and can be used for combined comparisons, in most cases when sorting is needed.
This 3-way comparison operator can already be found in other popular programming languages like Perl and Ruby.
Another new operator onboard is the Null Coalescing Operator. It’s denoted by two question marks (??).
It allows developers to check if something exists. If it exists and is not null, the operator will return the result of its first operand, and if it doesn’t or in any other case, it will return the second operand.
Here’s how the new operator reduces the time spent with basic declarations:
As in other object-oriented languages like C# and Java, developers can now use anonymous classes with PHP 7 as well.
The syntax of an anonymous class is the same as that of traditional PHP classes, the only difference being the missing name.
The main purpose of the anonymous classes is to speed up coding and execution times.
They do a great job in cases when a class is used once during execution or when a class doesn’t need to be documented.
Here is an example of an anonymous class as illustrated on PHP.net:
Developers can leverage the amazing back-end coding and real-world performance gains of the new PHP release by just upgrading their current PHP version.
There is no need for them to change a single line of code.
Developers can get the best of the new PHP without making any investment and will need fewer machines to serve the same amount of users.
These are just a few of the spectacular new features introduced in PHP 7.
Here is a list of other noteworthy features you will find in the long-awaited release:
- Abstract Syntax Tree
- Facilitated error handling through exceptions – the new Engine Exceptions will allow devs to replace fatal and catchable fatal errors with exceptions, which are differentiated from the traditional \Exception Base Class exceptions. Both exceptions can be used through the newly introduced \BaseException shared parent class.
- Secure random number generator
- Facilitated import from the same namespace – through the new Group Use Declarations syntax, developers will be able to import many classes from the same namespace, which makes the code tidier and saves a lot of typing time.
- Removed old and unsupported SAPIs & extensions – to free up space for improvement, many deprecated functionalities and unsupported Server APIs and extensions have been removed.
- Consistent 64-bit Windows OS support – although it is a LAMP member, through its latest version, PHP now supports native 64-bit integers and large files, allowing devs to run PHP on a 64-bit Windows platform as well.
You can find more information on the PHP 7’s official release on the official website of the community.
How to enable PHP 7 for my sites and applications?
You can upgrade to PHP 7 with a click of the mouse.
Just go to your Web Hosting Control Panel and select PHP Settings from the Advanced menu.
The ‘Choose a PHP version’ option will appear at the top of the page: