Talking about the important new characteristic _php skill of php7

Source: Internet
Author: User
Tags parse error scalar zend

So far, the official PHP has released the RC5 version of PHP7, is expected to release the first official version around November! For now, PHP7 's major features must have been stereotyped, and there will be no further changes. Later iterations are mainly about fixing bugs, optimizations, and the like. Here's what we've been looking forward to. The PHP7 will have those major changes ...

New feature Preview

Zend Engine upgrades to Zend Engine 3, which is called PHP NG
Add abstract syntax tree, makes compiling more scientific
64-bit int supports
unified variable Syntax
soundtrack TLS-meaningful
for extended development Improvements to the consistent foreach Loop
new <=>, * * 、?? 、 \u{xxxx} operators
Add a declaration of a return type increase the declaration of a
scalar type the
core error can be captured by an
exception Increased context-sensitive lexical analysis

Some features of removal

1. Removed some old extensions and moved to PECL (for example, MySQL)
2. Removal of Sapis support
3.<? Tags such as language= "PHP" are removed
The 4.16-in string conversion was abolished

PHP5
"0x10" = "//PHP7"

"
0x10"!= "16"


5.http_raw_post_data removed (can be replaced with php://input)
6. Static functions no longer support the invocation of a non-static function via an incompatible $this.
$o = & New classname{}, this is no longer supported
7.php.ini file removed # as a comment, uniform;

Some changes in behavior

parameter not defined with same name in support function
A constructor with the same name as a type is not recommended (no removal is currently removed and subsequent removal is removed)
The keywords string, int, float, and so on cannot be used as class names
Func_get_args () Gets the value of the current variable

function test ($num) {
  $num + +;
  Var_dump (Func_get_args () [0]);
};

Test (1)

//php5
Int (1)

//php7
int (2)

Here is a selection of some of the main, core, for our phper more important characteristics of the introduction

PHP NG

The new PHP engine optimizes a lot of places, and because of this, it makes PHP7 nearly twice times higher than PHP5 performance!

Reconstruction of Zval structure

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

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

Each member variable in the C language has to occupy a separate memory space, and the member variable in the union is a shared memory space (PHP7 is used to replace struct in a struct). Therefore, although the member variable looks a lot more, but the actual occupied memory space is a lot of common but declined.

Replace the previous Hashtale structure with the new Zend array

Our PHP program uses the most, most useful, most convenient, the most flexible is the array, and php5 its bottom is Hashtable implementation, PHP7 use of the new Zend array type, performance and access speed have been greatly improved!
Some of the most commonly used, inexpensive functions directly become engine-supported opcode

Call_user_function (_array) => zend_init_user_call
is_int/string/array/* => Zend_type_check
= > Zend_strlen
defined => zend+defined

Uses new memory allocations, manages the way, reduces the memory waste
Optimization of core sequencing Zend_sort

PHP5-fast sort (unstable sort)
Array (1 => 0, 0 => 0)

//php7-Quick sort + Select sort (Stable sort)
Array (0 => 0, 1 => 0)

Use selection for less than 16 elements, greater than 16 to split by 16 units, using the Select Sort, and then all together to use a quick sort. Compared to the previous, the internal elements from the unstable sort into a stable sort, reduce the number of elements exchanged, reduce the number of memory operations, performance improvement 40%
Abstract syntax Tree

If we now have such a demand, to the PHP source file on the line syntax detection, to achieve coding specifications. PHP5 before, no AST, directly from parser to generate opcodes! You need to use some external PHP parser to do this, and PHP7 added AST, we can do it ourselves to implement such an extension, using the extension provided functions can directly get the file corresponding to the AST structure, and this structure is what we can identify, So you can do some optimization and judgment on this basis.

64-bit INT support

Support for storing strings larger than 2GB
Supports uploading files of size greater than 2GB
Ensure that the string "64 bits" 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 ']}

The improvement of the 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: The internal pointer that no longer operates the data
$a = Array (1, 2, 3), foreach ($a as $v) {Var_dump ( $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)

A few new operators

<=>-Compare the size of two numbers "-1: The former is less than the latter, 0: The former equals the latter, 1: The former is greater than the latter"
Echo 1 <=> 2;//-1
echo 1 <=> 1;//0
Echo 1 & lt;=> 0;//1

//* *-"B" of "A"
Echo 2 * * 3;//8

//??-improvement of ternary operators
//php5
$_get[' name ']? $_get[' name ': ';//notice:undefined index: ...
PHP7
$_get[' name '?? '-> ';

\U{XXXX}-Parse
echo "\u{4f60}" for Unicode characters;//You
echo "\u{65b0}";//New

Returns the declaration of a 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 

Declaration of a scalar type

function getInt (int $num): int {return
  $num;
};

GetInt ("test");

PHP5
#PHP catchable fatal error:argument 1 passed to GetInt () must is a instance of int, string given ...

PHP7
#Fatal error:uncaught typeerror:argument 1 passed to GetInt () must is of the type Integer, string given ...

The core error can be captured by an exception

try {
  non_exists_func ();
} catch (Engineexception $e) {
  echo Exception: {$e->getmessage ();} \ n ";
}

Here with PHP7 tried a bit still can't capture, but see bird brother Introduction said is feasible ...
#Exception: Call to undefined function non_exists_func ()

Lexical analysis of up and down questioning sensitivity

PHP5
class Collection {public function foreach ($arr) {}}
#Parse error:parse error, expecting ' "identifier (t_s TRING) "' ...

PHP7
class 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 ();

Almost, basically put their own preliminary understanding of the PHP7, which certainly has a lot of wrong, low-level errors, I hope that all brothers timely correction, I can change, make a note! 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.