Find Out What’s New In PHP7
PHP 7 is the single most important evolution of the Internet’s most used programming language since the release of PHP 5 back in 2004. After many failed attempts at releasing PHP 6, it was finally decided to skip from 5 to 7 and avoid the confusion that could otherwise arise due to number of publications about PHP 6 that are already available. The newest version brings with it major improvements in performance, memory consumption, and introduces many long-awaited features.
This article briefly introduces some of the most important additions coming with PHP 7.
PHP 7 Is Much Faster
The one change that’s welcomed by everyone is the dramatic increase in performance. This is largely achieved thanks to refactoring of PHP’s large codebase, also referred to as phpng. This new branch specifically targets performance and memory usage efficiency of PHP.
What does it mean in practice? Magento should be able to handle 3x the number of requests compared with PHP 5.6 running on the same hardware and WordPress 3.6.0 home page requires 4x fewer CPU instructions.
Scalar Type Declarations
Another highly publicized addition to PHP 7 are scalar type hints. This slightly controversial feature has been discussed several times before it was accepted with 69% of the vote. No new reserved words have been added, as the names int, float, string, and bool are recognized and allowed as type declarations.
Return Type Declarations
PHP 7 enables developers to declare the return type of a function. This means that it will no longer be needed to use comments for documenting return types. This doesn’t in any way affect older code, which will still continue to function even without the explicit declaration of a return type.
Better Error Handling
Engine Exceptions are now handled differently and existing fatal errors have been replaced with exceptions. For example, in the case of incompatible data types, PHP 7 will display the following exception: Uncaught TypeException: Return value of add() must be of the type integer, float returned. Those familiar with PHP will probably remember a special kind of error reporting notice E_STRICT. PHP 7 removes this error category and replaces it with either E_DEPRECATED, E_NOTICE, or E_WARNING.
Null Coalesce Operator ??
The null coalesces operator allows programmers to chain together a string of values. This operator reads values from left to right and returns the first value that exists and is not null. It can be used as a fallback when reading user’s input.
Spaceship Operator <=>
The Spaceship operator (also known as the combined comparison operator) performs a three-way comparison by taking two values A and B that belong to a type with a total order and determining whether A < B, A = B, or A > B in a single operation. Essentially, the operator works just like its counterparts in Perl or Ruby.
Cryptographically Secure Pseudorandom Number Generator (CSPRNG)
It’s not easy to generate a sufficiently random number for cryptographic purposes. So far, the preferred solution in previous version of PHP was to use openssl_random_pseudo_bytes() or mcrypt_create_iv(). However, both functions require an activated extension. That’s why PHP 7 introduces two functions for random number generation in user-land:
1. random_bytes(int length);
2. random_int(int min, int max);
Removal of Deprecated Functionality
Additionally, anybody who is concerned about backwards compatibility with previous versions of PHP should carefully study this list of deprecated functionality in PHP 7.
PHP 7 brings massive speed improvements and many important features that developers were wishing for since the release of PHP 5. This new version was warmly accepted by the whole community and next months will be marked by a migration from older versions. Everybody who uses PHP is strongly encouraged to upgrade as fast as possible and avoid relying on outdated, slow versions.