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

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...

Command Prompt VS 2010: Fix MSVCRT.lib(MSVCR100.dll) : error LNK2005: xxx already defined in LIBCMT.lib(xxx.obj)

One of my task is to use Command Prompt of Visual Studio 2010 to evaluate the assignments of students. This assignment is about pointer and memory management in C++. The students are required to use new and delete operator in C++ to manage the memory and they need to ensure that the number of new callings must be equal to the number of delete callings. I overload the new and delete operator, build it into a static library (.lib file) and require student to use this .lib file when implementing the assignment. There is no problem when building the .lib file in VS 2010 project, but there are errors when compile the source code if I link the source code to the above static .lib file. Here is the format of error: MSVCRT.lib(MSVCR100.dll) : error LNK2005: xxx already defined in LIBCMT.lib(xxx.obj) Fortunately, I found 2 ways to fix this error on the internet. The first one is to remove the default lib when compiling the source code, it's libcmt.lib, because this libcmt.lib alread...

Fix cross domain in ajax request

Follow this sample ajax request $.ajax({    "type": 'POST',    "url": url,     "crossDomain": true,    "data": params,    "dataType": 'json',    "async": false,    "success": function(data){ done_fn(data, is_test);    },    "error": function (jqXHR) { var data = $.parseJSON(jqXHR.responseText); $("#results").text(data.message);    } }); The keys are crossDomain: true and async: false Hope to help many people.