This year will be the twentieth anniversary of PHP’s first release. Back in 1995, its creator, Rasmus Lerdorf, could have had no conception of the impact it would have. What was originally called Personal Home Page Tools is now the foundation of much of the modern web. WordPress alone accounts for 24% of all websites. Publishers, eCommerce merchants, and businesses the world over rely on PHP every day. Many thousands of developers make their living from PHP. All of which means that the imminent release of new major version of PHP is a big deal.
PHP 7, which is currently in beta and is expected to have its final release at the end of this year, brings some significant changes. But first, why PHP 7? The current stable version of PHP is 5.6. There will be no public release of PHP 6, which briefly existed as a development project, but was abandoned before it reached completion. In order to avoid confusion with the aborted version, the new release will skip 6 and go straight to 7.
PHP 7 Is Fast!
PHP has never been known as a speed demon, which has had an impact on the performance of applications like WordPress and Magento, both of which would benefit from a boost. The poor performance of PHP is what prompted Facebook to build their faster HHMV engine.
PHP 7 is much faster than PHP 5.X. It’s based on a completely refactored Zend engine which provides substantially improved performance. Benchmarks should be taken with a pinch of salt, but most tests show PHP 7 performing about as well as HHMV, with speed increases of anywhere between 25% and 70%, which is great news for performance critical applications.
PHP 7 introduces a couple of new operators: the null coalescing operator and the so-called “spaceship operator”.
The first of these, which is denoted with a pair of question marks (??) is essentially an existence check. It allows developers to return a default value if its first operand doesn’t exist, which is a handy shortcut for a common coding task.
The spaceship operator (<=>) is a new comparison operator: it takes a pair of operands and returns a different value depending on whether the operands are equal (0), the left is greater (1), or the right is greater (-1). It’s nothing that couldn’t be done before, but it reduces the complexity of the code required to carry out this type of comparison.
Scalar Type Hints
A major cause of inscrutable bugs in PHP code is the lack of an ability to specify the types of values that could be passed to and returned from functions. The wrong type wouldn’t necessarily cause an error, but could cause unpredictable behavior, which is the last thing a developer wants.
Unpredictability makes it difficult for developers to reason about their code and understand its behavior. This sort of bug is also quite difficult to track down.
Scalar type hints prevent the passing and returning of incorrect types.
Other goodies in PHP 7 include a range of new exceptions for what would previously have caused fatal errors, anonymous classes, and, of course, hundreds of bug fixes and optimizations.