


Let's take a look at the new features and performance optimizations of php7 and PHP5.
Recommended (free): PHP7
1. Abstract syntax tree (AST)
1) In PHP5, the execution process from php script to opcodes is:
- Lexing: Lexical scanning analysis, converting source files into token streams;
- #Parsing: Syntax analysis, generating op arrays at this stage.
2) In PHP7, op arrays are no longer directly generated during the syntax analysis phase, but AST is generated first. , so there is one more step in the process:
- Lexing: lexical scanning analysis, converting the source file into a token stream;
- Parsing: Syntax analysis, generates abstract syntax tree from token stream;
- Compilation: generates op arrays from abstract syntax tree.
Added abstract syntax tree: memory usage increased, but execution time decreased
AST plays the role of a middleware in the PHP compilation process, replacing the original method of spitting out opcode directly from the interpreter, decoupling the interpreter (parser) and the compiler (compliler). It can reduce some Hack code, and at the same time, make the implementation easier to understand and maintain
2. Natice TLS
##PHP needs to solve the problem of "Thread Safety" (TS, Thread Safe) in multi-threaded mode. Because threads share the memory space of the process, each thread itself needs to be constructed in some way. Private space to save your own private data to avoid mutual contamination with other threads.
The method adopted by PHP5 is to maintain a global large array and allocate an independent storage space to each thread. The threads access this global data group through their own key values. . This unique key value needs to be passed to every function that needs to use global variables in PHP5. PHP7 believes that this method of passing is not friendly and has some problems. Therefore, try to use a global thread-specific variable to save this key value.
3. Specify the parameter return value type
A very important feature of the PHP language is "weak typing". It makes PHP programs very easy to write.
PHP7 optionally supports type definition. In addition, it also introduces a switch instruction declare(strict_type=1); , once this instruction is turned on, it will force the program under the current file to follow strict function parameter types and return types.
4. Changes in zval structure
In PHP5, zval is defined as follows:struct _zval_struct { union { long lval; double dval; struct { char *val; int len; } str; HashTable *ht; zend_object_value obj; zend_ast *ast; } value; zend_uint refcount__gc; zend_uchar type; zend_uchar is_ref__gc; };
First of all, the size of this structure is 24 bytes (on a 64-bit system). Let's take a closer look at this zval.value union. zend_object_value is the largest long board, which causes the entire value to require 16 words. section, this should be easy to optimize, such as moving it out and replacing it with a pointer, because after all, IS_OBJECT is not the most commonly used type.
Second, each field of this structure has a clear meaning, and no custom fields are reserved. As a result, when doing a lot of optimizations in the PHP5 era, some information related to zval needs to be stored. You have to use other structure mapping, or external packaging and patching to expand zval. For example, in 5.3, a new GC was introduced to specifically solve circular references. It must not adopt the following relatively hacky approach
Third, most of PHP's zvals are passed by value, and the value is copied when writing. However, there are two exceptions, which are objects and resources. They are always passed by reference, so This creates a problem. In addition to the reference count in zval, objects and resources also need a global reference count to ensure that the memory can be recycled. So in the era of PHP5, taking objects as an example, it has two sets of reference counts. One is in zval, and the other is the count of obj itself:
第四, 我们知道PHP中, 大量的计算都是面向字符串的, 然而因为引用计数是作用在zval的, 那么就会导致如果要拷贝一个字符串类型的zval, 我们别无他法只能复制这个字符串. 当我们把一个zval的字符串作为key添加到一个数组里的时候, 我们别无他法只能复制这个字符串. 虽然在PHP5.4的时候, 我们引入了INTERNED STRING, 但是还是不能根本解决这个问题.
还比如, PHP中大量的结构体都是基于Hashtable实现的, 增删改查Hashtable的操作占据了大量的CPU时间, 而字符串要查找首先要求它的Hash值, 理论上我们完全可以把一个字符串的Hash值计算好以后, 就存下来, 避免再次计算等等
第五, 这个是关于引用的, PHP5的时代, 我们采用写时分离, 但是结合到引用这里就有了一个经典的性能问题:
第六, 也是最重要的一个, 为什么说它重要呢? 因为这点促成了很大的性能提升, 我们习惯了在PHP5的时代调用MAKE_STD_ZVAL在堆内存上分配一个zval, 然后对他进行操作, 最后呢通过RETURN_ZVAL把这个zval的值”copy”给return_value, 然后又销毁了这个zval, 比如pathinfo这个函数:
5、异常处理
PHP 5 的 try ... catch ... finally 无法处理传统错误,如果需要,你通常会考虑用 set_error_handler() 来 Hack 一下。但是仍有很多错误类型是 set_error_handler() 捕捉不到的
PHP 7引入 Throwable 接口,错误及异常都实现了 Throwable,无法直接实现 Throwable,但可以扩展 \Exception 和 \Error 类。可以用 Throwable 捕捉异常跟错误。\Exception 是所有PHP及用户异常的基类;\Error 是所有内部PHP错误的基类。
$name = "Tony"; try { $name = $name->method(); } catch (\Error $e) { echo "出错消息 --- ", $e->getMessage(), PHP_EOL; } try { $name = $name->method(); } catch (\Throwable $e) { echo "出错消息 --- ", $e->getMessage(), PHP_EOL; } try { intp(5, 0); } catch (\pisionByZeroError $e) { echo "出错消息 --- ", $e->getMessage(), PHP_EOL; }
6、hashtable 的变化
7、执行器
8、新的参数解析方式
PHP5 对应的参数解析 zend_parse_parament,
PHP7对应的参数解析:fast_zpp
The above is the detailed content of Let's take a look at the new features and performance optimizations of php7 and PHP5.. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...
