PHP 7 is now available and these are the new features

Lees in het Nederlands - Lisez en français

PHP 7 is finally available... What a magnificent end of year gift for all developers!

The many PHP developers were really looking forward to discovering the new version of this programming language, particularly as it promised to deliver much better performance. But PHP 7 has much more to offer. Combell technology evangelist Thijs Feryn gives some explanation (click here for a technical version with more details and code examples on Thijs’s personal blog).

Lightning-fast with PHP 7 thanks to the PHP Next Generation engine

Lightning-fast with PHP 7 thanks to the PHP Next Generation engineThere is no need to tell you once again that PHP is very popular among developers. Back when it was created by Rasmus Lerdorf, in 1994, PHP stood for Personal Home Page. Today, the language has become an efficient and versatile tool for developing websites, partly thanks to the very active community of developers who constantly improve this language.

But this week, a new milestone was reached in the evolution of this language: PHP version 7.0 has been released. The first thing we notice is its impressive performance, which results from the new PHP Next Generation engine, the successor of the old Zend Engine. This new engine is claimed to be twice as fast as the previous version, PHP 5.6.

Scalar type hints and return type declarations

However, this new version has much more to offer. You can e.g. use scalar type hints. Originally, PHP was a loosely typed language, which means that variables are not declared with strict data types, so that a variable in a script can be assigned a value of a different type than the one of the original value.

That makes PHP extremely flexible, but sometimes also unreliable, because errors are not reported immediately. This is why, over time, type hints have been added: if a value does not match the type hint, a warning is thrown. And that was a great idea, but until now these type hints were limited to objects, arrays and callables.

This is why PHP 7 presents scalar type hints: integers, strings, booleans and floats can now also be used as type hints. Moreover, return type declarations are now supported. As a developer, this allows you to make sure that the output of functions and methods is consistent.

When you run PHP in standard mode (coercive), PHP will only report errors when it returns a wrong type that cannot be converted to the desired type. The string “12” will e.g. be allowed if the interface of the function or method expects an integer. But if you activate strict mode, PHP will be less forgiving and throw an error when the types do not completely match.

Operators: Nulls... and Space ships

PHP 7 Operators and space shipsPHP 7 also introduced a couple of new operators: the null coalesce operator and the spaceship operator.

The null coalesce operator is a ternary operator, which checks whether the evaluated value exists and if it is not null. If it is the case, the value is returned. If not, an alternative value is returned.

As for the spaceship operator (we really love that name!), it makes it possible to define whether a value is greater than, less than or equal to another value. This is expressed with 0 (equal to), -1 (less than) or 1 (greater than).

Anonymous classes

We should also mention classes. In PHP’s object-oriented model, classes are explicitly declared, and later explicitly instantiated to objects. From PHP 7, it is possible to declare anonymous classes. These are classes that are declared on the fly, much like anonymous functions ( ~ callbacks) that have been available in PHP 5 for a while now.

But... what about PHP 6?

PHP 7 is here and this is what you should knowAttentive readers will probably wonder why this new version is named PHP 7.0, given that the previous version was named PHP 5.6. So, what exactly happened to PHP 6? Well, PHP 6 has definitely existed, but it has never been released. The main purpose of PHP 6 was indeed to provide full Unicode support, also at the level of the language itself. But that goal appeared to be impossible to reach and PHP 6 was pronounced dead. The other features that PHP 6 should have introduced were added to PHP 5.3.

Although PHP 6 has never seen the light of day, the project did exist within the PHP core group. This is precisely why the decision was taken to name the new version PHP 7.

More info?

PHP offers many other new features that are too numerous to discuss in this article. Feel free to visit http://php.net/manual/en/migration70.new-features.php for a complete overview.

Our colleague, Thijs, has once again lived up to his reputation as a technology evangelist at Combell: he made a video to explain the main features using code examples.

He also wrote a technical blog post about it with additional details and code examples.

Do you want to use PHP 7 on your hosting account?

That is possible! We already have PHP 7.0.0 available on our shared hosting packages. Go to your My Combell control panel to migrate from PHP 5 to PHP 7. Do you want to use PHP 7 on your Cloud server? That is possible too! Please contact our helpdesk. If necessary, our collaborators can compile PHP 7 on your system.

Click here to order PHP 7 hosting

One last thing: PHP 7 also includes a few backwards incompatible changes. Go to http://php.net/manual/en/migration70.incompatible.php and check whether your code is compatible with these changes.

Good luck with your lightning-fast PHP!