Love it or hate it — and there are plenty of developers on both sides of that potential flamewar — it’s undeniable that PHP has become an essential underpinning of the modern web. Many of the web’s most important applications and sites are based on PHP: WordPress, Magento, Facebook, and of course, InterWorx, to name just a small fraction. Back in 1995, when what was to become PHP was first released, its creator Rasmus Lerdorf thought of is as a simple C API that made it easier to carry out some common web development tasks. Twenty years later, what was once quaintly known as Personal Home Page Tools, is seeing a renaissance thanks to tools like Composer.
This year — in October if all goes to plan — PHP will get its first major version bump since 2004 when PHP 5 was released, although PHP 5’s minor releases have seen many great new features added over the years. PHP 7 (there will be no PHP 6) incorporates thousands of changes, but I’d like to focus on three headline features that will make a significant difference to the day-to-day experience of PHP developers and their users.
This is the big one. PHP 7 will be based on PHPNG, which was a version of PHP based on a refactored Zend Engine. The focus was on performance and memory improvements, and testing shows that the work has paid off handsomely.
Performance hikes of between 25% and 70% have been seen. As an example, executing WordPress’ homepage on earlier versions of WordPress took around 9.5 billion CPU instructions. PHP 7 does it in approximately 2.5 billion — a saving of more than 70%. The exact improvements depend on the benchmark and specific application environment, but in many applications PHP 7 has been shown to be on par with Facebook’s HHVM.
Scalar Type Hints
Scalar type hints allow developers to specify the types of values that can be used in function parameters and return values. They help developers avoid a specific set of programming errors that occur when the wrong value type is passed to a function. Previously, using the wrong value would not necessarily lead to an error and could result in unpredictable behavior in applications. Scalar type hints make it “easier [for developers] to reason about code”.
If you want to know more about scalar type hints, Jose Luis Laso has written an excellent summary.
The Spaceship Operator
PHP 7 gets a new comparison operator. The <=> operator returns a value based on the results of a comparison that indicates how the operands differ. If the left operand is greater, it returns 1. If the right is greater, it returns -1. And if they are equal, it returns 1.
The spaceship operator is especially useful for sorting functions, allowing developers to quickly make comparisons they would previously have to have written more extensive code to carry out.