PHP7 new Features

Source: Internet
Author: User
Tags hhvm php language scalar zend

PHP7

Two events took place in 2015.12.3, and PHP7 was unveiled, and Swift was open source.

The best language released a new version, an epoch-making big version: PHP7.

PHP7 fixed a number of bugs, added features and syntax sugar. These changes are related to core packages, GD libraries, PDO, ZIP, ZLIB and other familiar and unfamiliar core functions and expansion packs.

PHP7 removed functions that have been discarded, such as Mysql_ series functions, are discarded in PHP5.5 and deleted in PHP7.

PHP7 performance is higher than HHVM. And it's twice times the PHP5.6.

Http://php.net/archive/2015.php#id2015-12-03-1

December 3, 2015

The PHP development team announced that PHP 7.0.0 is coming soon. This release marks the beginning of the new important PHP 7 series.

PHP 7.0.0 comes with a new version of the Zend engine, with countless improvements and new features such as

Performance improvements:

PHP 7 up to twice times faster PHP 5.6

Significantly reduced memory usage

Abstract syntax Tree

Consistent 64-bit support

Improved exception hierarchies

Many conversions to fatal errors

Safe Random number generator

Remove old and unsupported Sapis and extensions

Empty merge operator (? )

Return and scalar type declarations

Anonymous class

0 Cost Assertion

This is the next major version of PHP. Its release is the result of the development journey of the last two years. This is a very special achievement for the core team. Moreover, it is the result of incredible efforts by many active community members. In fact, this is the rise of a new generation of PHP with great potential.

Congratulations to everyone, this is a spectacular PHP world!

1.PHP7 is twice times faster than PHP5.6.


2.jit-just in time compiler (instant editor)
Just in Time (instant compilation) is a software optimization technique that compiles bytecode into machine code at runtime. From intuition, it is easy to think that the machine code is directly recognized and executed by the computer, which is more efficient than zend read opcode-by-article execution. Among them, HHVM (HipHop virtual MACHINE,HHVM is a Facebook open-source PHP virtual machine) to use the JIT, so that their PHP performance test to improve an order of magnitude, releasing a shocking test results, It also makes us intuitively think that JIT is a powerful technology with Midas touch.

In fact, in 2013, Brother Bird and Dmitry (one of the PHP language kernel developers) had a JIT attempt on the PHP5.5 version (not published). PHP5.5 's original execution process, is the PHP code through lexical and syntactic analysis, compiled into opcode bytecode (format and assembly a bit like), and then, Zend Engine read these opcode instructions, one by one parse execution.

They introduce type inference (typeinf) after the opcode link, and then generate Bytecodes by JIT and then execute.


3.Zval of Change

PHP's various types of variables, in fact, the real storage of the carrier is Zval, which is characterized by the sea of the hundred rivers, the capacity is large. Essentially, it is a struct (struct) implemented by the C language. For a classmate who writes PHP, it can be roughly understood to be something like an array of arrays.

PHP5 of Zval, Memory occupies 24 bytes:
PHP7 of Zval, Memory occupies 16 bytes:

Zval from 24 bytes down to 16 bytes, why would fall, here need to fill a little bit of C language Foundation, auxiliary unfamiliar C classmate understand. Structs and unions are a bit different, each member variable of a struct has to occupy a separate memory space, and the member variable in the union is a shared memory space (that is, modifying one of the member variables, the public space is modified, The record of the other member variables is gone). Therefore, although the member variable looks a lot more, but the actual occupied memory space is decreased.

In addition, there are features that are significantly changed, and some simple types no longer use references.


4. Internal type zend_string

Zend_string is the actual structure that stores the string, the actual content is stored in Val (char, character), and Val is a char array with a length of 1 (convenient member variable placeholder).

struct the last member variable takes a char array instead of using char*, here is a small optimization technique that can reduce the CPU cache miss.

If you use a char array, when malloc applies to the above structure, it is applied in the same area, usually the length is sizeof (_zend_string) + the actual char storage space. However, if you use char*, that location stores just one pointer, and the real storage is in a separate area of memory.

From the point of view of logical implementation, the two actually do not have much difference, the effect is very similar. In fact, when these memory blocks are loaded into the CPU, it is very different. The former because it is a contiguous allocation of the same piece of memory, when the CPU reads, usually can be obtained together (because it will be in the same level cache). While the latter, because it is two pieces of memory data, when the CPU reads the first memory, it is likely that the second block of memory data is not in the same cache, so that the CPU has to L2 (level two cache) below the search, and even to the memory area to find the second block of memory data. This will cause CPU Cache Miss, which can take up to 100 times times the time difference.

In addition, when the string is copied, the reference assignment is used to zend_string the memory copy that can be avoided.


Changes in 5.PHP arrays (Hashtable and Zend Array)

In the process of writing PHP programs, the most frequently used types are arrays, and PHP5 arrays are implemented using Hashtable. If it is a rough generalization, it is a hashtable that supports doubly linked lists, which not only supports the hash map access element through the array key, but also iterates through the array elements in a way that provides access to the doubly linked list through foreach.

The diagram looks very complex, and various pointers jump and jump, and when we access an element's content through a key value, sometimes it takes 3 of a pointer jump to find what's needed. The most important thing is that these array element stores are scattered across different memory areas. Similarly, when the CPU reads, because they are most likely not in the same level cache, the CPU will have to go to the lower cache or even the memory area to find, that is, to cause the CPU cache hit down, which adds more time-consuming.

The new version of the array structure, very concise, let a person in front of the light. The most important feature is that the entire array element and hash mapping table are all connected together and are allocated in the same piece of memory. It is very efficient to iterate through an array of simple types of an integral type, because the array element (Bucket) itself is continuously allocated in the same piece of memory, and the zval of the elements of the array will store the integral element inside, no longer have the pointer outside the chain, and all the data is stored in the current memory area. Of course, the most important thing is that it avoids CPU cache Miss (the CPU buffer hit rate drops).


6. Function call mechanism (functions calling convention)
PHP7 improves the function's calling mechanism, and by optimizing the transfer of parameters, some instructions are reduced and the execution efficiency is improved.


7. Let the compiler do some work ahead of time by macro definition and inline function (inline)
C language macro definition will be in the pre-processing phase (compile phase) execution, the part of the work in advance, do not need to allocate memory when the program runs, can implement functions like functions, but there is no function call of the stack, the cost of the stack, the efficiency will be relatively high. Inline functions are similar, in the preprocessing phase, the functions in the program are replaced with the function body, the real-running program executes here, there is no cost of function calls.

PHP7 has done a lot of optimizations in this area, putting a lot of work that needs to be done during the run phase to the compile stage. For example, the parameter type of the judgment (Parameters parsing), because this is involved in a fixed character constants, so it can be put into the compilation phase to complete, and thus improve the subsequent execution efficiency.

PHP7 new features what'll be in PHP 7/phpng
http://blog.csdn.net/hguisu/article/details/45094079

Some new features of PHP7

1. Operator (NULL merge operator)

Put this on the first one because I think it's very useful. Usage:

$a = $_get[' A ']?? 1;

It is equivalent to:

<php
$a = isset ($_get[' a ')? $_get[' A ']: 1;

We know that ternary operators can be used in this way:

$a?: 1

But this is based on the premise that the $a has been defined. What's new?? Operators can simplify judgment.

2. function return value type declaration

An example of an official document (note ...) The edge length parameter syntax is available in PHP version 5.6 or higher):

<php
function Arrayssum (array ... $arrays): array
{
Return Array_map (function (array $array): int {
Return Array_sum ($array);
}, $arrays);
}

Print_r ([Arrayssum], [4,5,6], [7,8,9]);
As you can see from this example, functions (including anonymous functions) can now specify the type of the return value.

The wording of this statement is somewhat similar to Swift:

Func SayHello (personname:string), String {
Let greeting = "Hello," + PersonName + "!"
return greeting
}
This feature can help us avoid some of the problems with the implicit type conversion of PHP. It is possible to avoid unnecessary errors by thinking about the expected results before defining a function.

But there is also a feature to note. PHP 7 Adds a DECLARE directive: Strict_types, which uses strict mode.

When using a return value type declaration, if the return value is not the expected type, then PHP enforces the type conversion if it is not declared as strict mode. However, if it is a strict mode, it will start off with a TypeError Fatal error.

Mandatory mode:

<php
function foo ($a): int
{
return $a;
}

Foo (1.0);
The above code can execute normally, and the Foo function returns int 1 without any errors.

Strict mode:

<php
Declare (Strict_types=1);

function foo ($a): int
{
return $a;
}

Foo (1.0);
# PHP Fatal error:uncaught typeerror:return value of foo () must be's of the type integer, float returned in Test.php:6
After the declaration, a fatal error is triggered.

Isn't it a bit similar to JS's strict mode?

3. Scalar type declarations

The formal parameter type declaration of a function in PHP 7 can be a scalar. In PHP 5, only the class name, interface, array, or callable (PHP 5.4, which can be functions, including anonymous functions), can now also use string, int, float, and bool.

Official Example:

<php
Coercive mode
function sumofints (int ... $ints)
{
Return Array_sum ($ints);
}

Var_dump (sumofints (2, ' 3 ', 4.1));
It is important to note that the problem of the strict pattern mentioned above is equally applicable here: Forced mode (by default, coercion of type conversions), or coercion of type conversions for non-conforming parameters, which triggers TypeError fatal error in strict mode.

4. Use Batch Declaration

In PHP 7, use can declare multiple classes or functions or const in a sentence:

<php
Use Some/namespace/{classa, ClassB, ClassC as C};
Use function some/namespace/{fn_a, Fn_b, Fn_c};
Use const Some/namespace/{consta, CONSTB, CONSTC};
However, the name of each class or function or const is still written (and there is no way from some import * like Python).

The question to keep in mind is: if you're using a framework based on composer and PSR-4, does this kind of writing succeed in loading class files? In fact, the automatic loading method of composer registration is to find the location according to the namespace of the class when the class is called, which has no effect on it.

5. Other Features

Some of the other features I have not introduced, and are interested to view the official documents: http://php.net/manual/en/migration70.new-features.php

Briefly say a few:

PHP 5.3 begins with an anonymous function, and now it has an anonymous class;

Define can now define a constant array;

Closure (Closure) adds a call method;

The generator (or an iterator that is more appropriate) can have a final return value (return), or it can be entered in a different generator (generator delegate) through the new syntax of yield from.

The two new features of the generator (return and yield from) can be combined. We can test the concrete appearance. PHP 7 is now up to RC5, and the final version should come soon.

Extended reading:
Http://www.baidu.com/s?wd=php7%20 new Features
Http://www.sogou.com/web?query=php7%20 new Features
HTTPS://WWW.SO.COM/S?Q=PHP7 new Features

Http://developer.51cto.com/art/201510/494674.htm

Http://www.tuicool.com/articles/yARJRjQ

PHP7 new Features

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.