What Are The New Features Of PHP7? What Improvements Have Been Made?
PHP 7 is a milestone version. It not only adds many new features to make code writing more flexible, but also improves the underlying design and improves the efficiency of PHP execution.
What's New in PHP 7
The new features in PHP 7 mainly include the following:
-
Scalar type declaration.
-
Function return value type declaration.
-
Added null coalescing operator.
-
Added composite comparator.
-
Supports defining constant arrays via define().
-
Added support for anonymous classes.
-
Supports Unicode codepoint translation syntax.
-
Better closure support.
-
Provides filtering for unserialize().
-
Newly added IntlChar class.
-
The use statement is supported to import classes, functions and constants from the same namespace.
-
Added the divisible function intdiv().
-
session_start() supports receiving array parameters.
Improvements and optimizations in PHP 7
Compared with previous versions, PHP 7 has some improvements at the language syntax level and the underlying architecture level:
-
The improvements at the syntax level are mainly adding some new features, removing some extensions, changing error exception handling, etc.
-
In terms of the underlying structure, the Zval and Zend_String structures that store various variables have been changed, the HashTable of Zend Array has been optimized, and the calling mechanism of functions has been improved.
In particular, the improvement of the underlying structure has greatly improved the execution efficiency of PHP 7, making it about twice as fast as PHP 5.
PHP 7 introduces strict mode switch
PHP is a weakly typed language, but in PHP 7 to support the definition of variable types, a switch directive
declare(strict_type=1); was
introduced . Once this directive is enabled, it will force the program under the current file to follow strict function parameter types and return types. If strict_type is not enabled, PHP will try to convert it to the required type; after it is enabled, PHP will no longer perform type conversion, and an error will be thrown if the type does not match.
To use strict mode, a declare directive must be placed at the top of the file. This means that strictly declared scalars are configurable on a file basis. This directive affects not only the type declaration of the parameter, but also the return value declaration of the function.
PHP 7 improves error handling
In addition, in PHP 7, many fatal errors and recoverable fatal errors have been converted to exceptions to handle. These exceptions inherit from the Error class, which implements the Throwable interface (all exceptions implement this base interface).
This also means that some error-handling code in the previous code will not be triggered when an error occurs. Because in the PHP 7 version, the error handling mechanism that throws an exception is already used. (If the Error exception is not caught in the code, a fatal error is raised).
PHP 7 optimizes Zval
In 2013, a JIT (Just In Time, just-in-time compilation, a software optimization technology) attempt was made on the PHP 5.5 version.
The original execution flow of PHP 5.5 is to compile the PHP code into opcode bytecode through lexical and grammatical analysis, and then the Zend engine reads these opcode instructions and parses and executes them one by one. They introduced type inference (TypeInf) after the opcode link, and then generated ByteCodes through JIT for execution.
With this technical optimization, the efficiency of PHP has not been significantly improved in actual projects, so they redesigned the underlying language structure of PHP. Zval is a carrier for storing variables in PHP. It is a struct implemented in C language. The Zval of PHP 5 occupies 24 bytes in memory, while the optimized Zval in PHP 7 occupies only 16 bytes, so the storage of variables becomes very simple and efficient.
PHP 7 optimizes arrays
PHP 7 optimizes the HashTable implementation of arrays. The array storage form of PHP 5 is a HashTable that supports a doubly linked list. It not only supports hash mapping to access elements through the key of the array, but also traverses the array elements through foreach to access the doubly linked list.
When we access the content of an element through the key value, it sometimes takes 3 pointer jumps to find the desired content. The most important point is that the storage of these array elements is scattered in different memory areas. When the CPU reads, because they are likely not in the same level of cache, the CPU has to go to the lower-level cache or even the memory area to find, As a result, the CPU cache hits drop, which in turn increases more time-consuming.
The biggest feature of the optimized Zend Array is that the entire block of array elements and the hash mapping table are all connected together and allocated in the same block of memory. If it is to traverse a simple type array of integers, the efficiency will be very fast. Because the array element (Bucket) itself is continuously allocated in the same block of memory, and the Zval of the array element will store the integer element inside, and there is no longer a pointer external link, all data is stored in the current memory area.
Of course, the most important thing is that it can avoid CPU cache hit rate drop.
PHP 7 improves function calls
PHP 7 also improves the calling mechanism of functions, reduces some instructions by optimizing the link of parameter passing, and improves execution efficiency.