Home  >  Article  >  Backend Development  >  Let's take a look at the major new features of php7

Let's take a look at the major new features of php7

coldplay.xixi
coldplay.xixiforward
2020-06-28 16:26:423566browse


Let's take a look at the major new features of php7

So far, PHP has officially released the RC5 version of php7. Now the major features of php7 have definitely been finalized. , there will be no more changes. The iterations of some subsequent versions are mainly bug fixes, optimizations and the like. Let’s talk about the major changes in php7 that we have been looking forward to.

New feature preview

ZEND引擎升级到Zend Engine 3,也就是所谓的PHP NG
增加抽象语法树,使编译更加科学
64位的INT支持
统一的变量语法
原声的TLS - 对扩展开发有意义
一致性foreach循环的改进
新增 <=>、**、?? 、\u{xxxx}操作符
增加了返回类型的声明
增加了标量类型的声明
核心错误可以通过异常捕获了
增加了上下文敏感的词法分析

Related learning recommendations: PHP Programming from entry to master

#Some features removed

1. Some old extensions were removed and migrated to PECL (for example : mysql)
2. Remove support for SAPIs
3. Tags like 4. Hexadecimal string conversion has been abolished

//PHP5
"0x10" == "16"

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

5. HTTP_RAW_POST_DATA has been 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{} is no longer supported.
7.php.ini file has removed # as a comment and uses ; to comment

Some behavior changes

No longer supports function definition parameters with the same name
The constructor of the same type is no longer recommended (it has not been removed currently, and 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)

//PHP5
int(1)

//PHP7
int(2)

The following is a selection of some main and core ones that are more suitable for our PHPer Let me introduce the important features

PHP NG

The new php engine has optimized many places, and it is officially because of this that php7 has better performance than php5. Nearly twice the improvement!

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 complicated than that of 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 (union is widely used to replace struct in php7). Therefore, although it seems that there are a lot more member variables, the actual memory space occupied, many of which are public, has decreased.

Use the new Zend Array to replace the previous HashTale structure

The most used, most useful, most convenient and most flexible in our php program It is an array, and the bottom layer of php5 is implemented by HashTable. php7 uses the new Zend Array type, and the performance and access speed have been greatly improved!
Some very commonly used and low-cost functions directly become opcodes supported by the engine

call_user_function(_array) => ZEND_INIT_USER_CALL
is_int/string/array/* => ZEND_TYPE_CHECK
strlen => ZEND_STRLEN
defined => ZEND+DEFINED

Use new memory allocation and management methods to reduce the waste of memory
Core sorting zend_sort Optimize

//PHP5 - 快速排序(非稳定排序)
array(1 => 0, 0 => 0)

//PHP7 - 快速排序+选择排序(稳定排序)
array(0 => 0, 1 => 0)

Use selection sorting for elements less than 16, split them into units of 16 for elements larger than 16, use selection sorting respectively, and then combine them all and use quick sorting. Compared with the previous sorting, the internal elements have been changed from non-stable sorting to stable sorting, reducing the number of exchanges of elements, reducing the number of operations on memory, and improving performance by 40%
Abstract Syntax Tree

If we have such a need now, we need to perform syntax detection on the PHP source file and implement coding standards. Before php5, there was no AST, and opcodes were generated directly from the parser! We need to use some external php syntax parsers to achieve this; and php7 adds AST, we can implement such an extension ourselves, and use the functions provided by the extension to directly obtain the AST structure corresponding to the file, and this structure is what we It can be identified, so we can do some optimization and judgment on this basis.

64-bit INT support

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

$$foo[&#39;bar&#39;][&#39;baz&#39;]

//PHP5
($$foo)[‘bar&#39;][&#39;baz&#39;]

//PHP7: 遵循从左到右的原则
${$foo[‘bar&#39;][&#39;baz&#39;]}

Improvements 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:不再操作数据的内部指针了
$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)

Several new operators

//<=> - 比较两个数的大小【-1:前者小于后者,0:前者等于后者,1:前者大于后者】
echo 1 <=> 2;//-1
echo 1 <=> 1;//0
echo 1 <=> 0;//1

// ** - 【a的b次方】
echo 2 ** 3;//8

//?? - 三元运算符的改进
//php5
$_GET[&#39;name&#39;] ? $_GET[&#39;name&#39;] : &#39;&#39;;//Notice: Undefined index: …
//php7
$_GET[&#39;name&#39;] ?? &#39;&#39; -> &#39;&#39;;

//\u{xxxx} - Unicode字符的解析
echo "\u{4f60}";//你
echo "\u{65b0}";//新

Declaration of return type

function getInt() : int {
  return “test”;
};

getInt();

//PHP5
#PHP Parse error: parse error, expecting &#39;{&#39;...

//PHP7
#Fatal error:Uncaught TypeError: Return value of getInt() must be of the type integer, string returned

Declaration of scalar type

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 caught through exceptions

try {
  non_exists_func();
} catch(EngineException $e) {
  echo “Exception: {$e->getMessage();}\n”;
}

//这里用php7试了一下还是没法捕获,但是看鸟哥介绍说是可行的。。。
#Exception: Call to undefined function non_exists_func()

Sensitive lexical analysis

//PHP5
class Collection {public function foreach($arr) {}}
#Parse error: parse error, expecting `"identifier (T_STRING)”&#39;...

//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();

That's it, I basically finished my preliminary understanding of php7. There must be many wrong and low-level mistakes in it. I hope you guys can correct them in time so that I can correct them and take notes! hey-hey!

The above is the detailed content of Let's take a look at the major new features of php7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete