1. Performance
PHP 7 helped to reduce the memory usage and increase the performance. Some benchmarks show that the new performance is as twice as PHP 5.6.
In my case, the wordpress site of our company needed 25s to fully load, after I apply PHP 7, the loading time is only 8s.
Why is PHP 7 faster?
+ New core engine
PHP 4 uses Zend Engine (not confused with Zend framework)
PHP 5 uses Zend Engine II
PHP 7 uses PHPNG, a refactored Zend Engine II
The refactoring makes the new generation of engine consume less memory (bucket size, HashTable size, etc), less CPU resource.
2. Declare type for variables
In PHP 5 does not require developers to declare type. Type declaration helps developers to get the expected result. But in PHP 5, dev also can declare type using Type hinting, however, this is limited to function parameters declaration only and also limited to Class type and Array type.
function foo(Square $obj, array $colors){
}
Above function helps to declare a function foo with 2 parameters. You need to pass the right types for this function or you will get fatal error.
In PHP 7 now added Scalar types: int, float, string and bool. Scalar type declaration has 2 modes: non-strict and strict. By default, it is non-strict, which means you can pass a wrong type value to a variable, PHP 7 will try to cast the data. Otherwise, a fatal error will occur.
declare(strict_types=1);
Return type declaration:function getTotal(float $a, float $b) : float {
In overall, type declaration make your code more readable.3. Error handling
Warning and notice error handling does not change in PHP 7 but it does change in fatal error handling. Fatal error cannot be handled in PHP 7 and it will simply stope the execution.4. New operators
Spaceship operator <=>
$compare = 2 <=> 1 2 < 1? return -1 2 = 1? return 0 2 > 1? return 1
Null operator assign
$name = $firstName ?? "Guest";
It assigns left variable if it is not null, else return right one.5. Easy User-land CSPRNG
PHP 7 offers an secure and easy random method to use. It uses the operation system random generator so it is more secure than PHP 5.These random values are used to check the origin of the request from server side's rendered form.
Comments
Post a Comment