Major new features of PHP 7

Source: Internet
Author: User
PHP7 will be officially released in October 2015. PHP7 will be a major version update for the PHP script language. It will also bring about significant performance improvements and new features, as well as some outdated features. This version will focus on performance enhancement, derived from the phpng branch in the PHP version tree. So far, PHP has officially released the PHP 7 RC5 version. it is expected that the first official version will be released around May! Now, the major features of PHP 7 must have been finalized and won't be changed. Later versions of iterations mainly involve bug fixing and optimization. Let's talk about the major changes we have always expected in PHP 7...

New Feature Preview

The ZEND Engine is upgraded to Zend Engine 3, that is, the so-called php ng adds the abstract syntax tree, make compilation more scientific 64-bit INT support for unified variable syntax original TLS-improvements to meaningful and consistent foreach loop for extended development <=> ,**,?? The \ u {xxxx} operator adds the declaration of the return type, and adds the declaration of the scalar type. The core error can be caught by exception, and context-sensitive lexical analysis is added.

Removed features

1. remove some old extensions and migrate them to PECL (for example, mysql)
2. remove SAPIs support
3. <? And <? Labels such as language = "php" are removed.
4.16 hexadecimal string conversion is abolished

//PHP5"0x10" == "16"//PHP7"0x10" != "16"


5. HTTP_RAW_POST_DATA is removed (you can use php: // input instead)
6. static functions no longer support calling a non-static function through an incompatible $ this.
$ O = & new className {}. this method is no longer supported
7. the php. ini file is removed # used as a comment for unified use; commented out

Some behavior changes

Parameters with the same name cannot be defined in the function.
Type constructors with the same name are not recommended (not removed currently, will be removed later)
String, int, float, and other keywords cannot be used as class names.
Func_get_args () obtains the value of the current variable.

function test ($num) {  $num++;  var_dump(func_get_args()[0]);};test(1)//PHP5int(1)//PHP7int(2)

The following describes some of the main and core features that are important to PHPer.

PHP NG

The new php engine has been optimized in many places. because of this, php7 has improved the performance of php5 by nearly twice!

Reconstruction of ZVAL structure

The left side is the zval of PHP5 (24 bytes), and the right side is the zval of PHP7 (16 bytes );

It can be seen that the zval of php7 is more complex than php5, but it can be reduced from 24 bytes to 16 bytes. why?

In C language, each member variable of struct occupies an independent memory space, while the member variables in union Share a memory space (in PHP 7, a lot of union is used to replace struct ). Therefore, although the number of member variables seems to be much higher, many of the memory space actually occupied is public, but it drops.

Replace the previous HashTale structure with the new Zend Array

The most widely used, useful, convenient, and flexible php program is the Array, and php5 is implemented by HashTable at the underlying layer. php7 uses the new Zend Array type, performance and access speed have been greatly improved!
Some very common functions with low overhead directly become the opcode supported by the engine.

call_user_function(_array) => ZEND_INIT_USER_CALLis_int/string/array/* => ZEND_TYPE_CHECKstrlen => ZEND_STRLENdefined => ZEND+DEFINED

New memory allocation and management methods are used to reduce memory waste.
Optimization of core sorting zend_sort

// PHP5-quick sorting (non-stable sorting) array (1 => 0, 0 => 0) // PHP7-quick sorting + Select sorting (stable sorting) array (0 => 0, 1 => 0)

If the number of elements is less than 16, the selected sorting is used. if the number of elements is greater than 16, the elements are separated by 16 units. the selected sorting is used respectively, and then all elements are combined for quick sorting. Compared with the previous sorting, internal elements are converted from unstable sorting to stable sorting, reducing the number of element exchanges, reducing the number of memory operations, and improving the performance by 40%.
Abstract syntax tree

If we have such a requirement, we need to check the php source file in the syntax to implement the encoding specification. If there is no AST before php5, opcodes is generated directly from parser! We need to use some external php syntax parser to implement it. with the addition of AST in php 7, we can implement this extension by ourselves, by using the functions provided by the extension, you can directly obtain the AST structure corresponding to the file. this structure is exactly what we can identify, so we can perform some optimization and judgment on this basis.

64-bit INT support

Supports storing strings larger than 2 GB
Supports uploading files larger than 2 GB
Ensure that the string [64-bit] is 64bit on all platforms
Unified syntax variables

$ Foo ['bar'] ['Baz'] // PHP5 ($ foo) ['bar'] ['Baz'] // PHP7: follow the principle from left to right $ {$ foo ['bar'] ['Baz']}

Improvement of foreach loop

// PHP5 $ a = array (1, 2, 3); foreach ($ a as $ v) {var_dump (current ($ a);} int (2) int (2) int (2) $ a = array (1, 2, 3); $ B = & $ a; foreach ($ a as $ v) {var_dump (current ($ a);} int (2) int (3) bool (false) $ a = array (1, 2, 3 ); $ B = $ a; foreach ($ a as $ v) {var_dump (current ($ a);} int (1) int (1) int (1) // PHP7: internal pointer $ a = array (1, 2, 3); foreach ($ a as $ v) {var_dump (current ($ a)} int (1) int (1) int (1) $ a = array (1, 2, 3 ); $ B = & $ a; foreach ($ a as $ v) {var_dump (current ($ a) int (1) int (1) int (1) $ a = array (1, 2, 3); $ B = $ a; foreach ($ a as $ v) {var_dump (current ($ a)} int (1) int (1) int (1)

New operators

// <=>-Compare the size of two numbers (-1: The former is smaller than the latter, 0: The former is equal to the latter, and 1: echo 1 <=> 2; //-1 echo 1 <=> 1; // 0 echo 1 <=> 0; // 1 // **-[B power of a] echo 2 ** 3; // 8 //?? -Improvements to ternary operators // php5 $ _ GET ['name']? $ _ GET ['name']: ''; // Notice: Undefined index :... // Php7 $ _ GET ['name']? ''->''; // \ U {xxxx}-Unicode parsing echo "\ u {4f60}"; // you echo "\ u {65b0 }"; // New

Declaration of return type

function getInt() : int {  return “test”;};getInt();//PHP5#PHP Parse error: parse error, expecting '{'...//PHP7#Fatal error:Uncaught TypeError: Return value of getInt() must be of the type integer, string returned 

Scalar type declaration

function getInt(int $num) : int {  return $num;};getInt(“test”);//PHP5#PHP Catchable fatal error: Argument 1 passed to getInt() must be an instance of int, string given…//PHP7#Fatal error: Uncaught TypeError: Argument 1 passed to getInt() must be of the type integer, string given…

Core errors can be captured through exceptions.

Try {non_exists_func ();} catch (EngineException $ e) {echo "Exception: {$ e-> getMessage () ;}\ n ";} // I tried php7 and couldn't capture it, but it is feasible to see the introduction of laruence... # Exception: Call to undefined function non_exists_func ()

Upstream and downstream sensitive lexical analysis

//PHP5class Collection {public function foreach($arr) {}}#Parse error: parse error, expecting `"identifier (T_STRING)”'...//PHP7class Collection {  public function foreach($arr) {}  public function in($arr){}  public function where($condition){}  public function order($condition){}}$collection = new Collection();$collection->where()->in()->foreach()->order();

Basically, I have finished my preliminary understanding of php7. there must be many incorrect and low-level errors. I hope you can correct them in time. I 'd like to make some changes and take notes! Hey!

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.