Skip to main content

New features in PHP 7

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.



PHP 7 can handle 2580 requests per second comparing to 1400 requests per second (Drupal).
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

Popular posts from this blog

HTTP_REFERER

A friend of mine ask me if a server can know where we come from before we go to that server (referal link). YES. This information is the field "HTTP_REFERRER" in $_SERVER of the coming request. Here is an example: http://113.161.96.198/referal/ For the reason of SEO, some guys do not want any server know about this referal link that points to their own server. Here are some solutions: HTML5: Add norefer attribute   No REFERRER PHP redirect: <?php header( 'Location: http://113.161.96.198/referal/ ' ) ; ?> .... lot of solutions lol

PHP json_encode return empty array [] instead of empty object {}

Problem: Get below array for example.  $status = array ( "message" => "error" , "club_id" => $_club_id , "status" => "1" , "membership_info" => array (), ); This array will be encoded in json format echo json_encode($status); This function return json: {"message":"error","club_id":275,"status":"1","membership_info": [] } Notice the empty array [], it is not consistent, it should be an empty object {} {"message":"error","club_id":275,"status":"1","membership_info": {} } The cause: This problem is caused by the called function array(), which yields an empty array [] Solution: There are 2 solutions for this issue: Solution 1: Call new stdClass instead of array(), stdClass generates an empty object {} Solution 2: The above solution is complex in case there are plenty of arr

SQL query optimization

SELECT query {EXPLAIN | DESCRIBE | DESC}  We usually use this command to check to see if how many rows the query goes through. Especially for the INDEX column checking. Search LIKE '%key%' consumes more resource than LIKE 'key%' Denormalisation This database design helps to speed up the data reading. Because the SQL query no need to join data between tables.