Table of Contents
首先先来说下PHP7带来的新东西
1.类型的声明。
2.set_exception_handler() 不再保证收到的一定是 Exception 对象
4.新增操作符“??”
5.define() 定义常量数组
6.AST: Abstract Syntax Tree, 抽象语法树
7.匿名函数
8.Unicode字符格式支持(echo “\u{9999}”)
9.Unserialize 提供过滤特性
10.命名空间引用优化
然后再来看下PHP7带来的废弃
1.废弃扩展
2.废弃的特性
3.废弃的函数
4.废弃的用法
最后再来看下PHP7带来的变更
1.字符串处理机制修改
2.整型处理机制修改
3.参数处理机制修改
4.foreach修改
5. list修改
6.变量处理机制修改
7.杂项
如何充分发挥PHP7的性能
1.开启Opcache
2.使用GCC 4.8以上进行编译
3.开启HugePage (根据系统内存决定)
4.PGO (Profile Guided Optimization)
如何更好的写代码来迎接PHP7?
如何升级当前项目代码来兼容PHP7?
Home Backend Development PHP7 Comparison explains the optimization and improvement of PHP7

Comparison explains the optimization and improvement of PHP7

Jun 28, 2020 pm 03:35 PM
php7

Comparison explains the optimization and improvement of PHP7

此篇张是我对PHP7跟PHP以前版本做的一个对比。

我们来看下PHP更新出来的各种版本:

首先先来说下PHP7带来的新东西

1.类型的声明。

可以使用字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool),来声明函数的参数类型与函数返回值。

代码:

declare(strict_types=1);
function add(int $a,int $b):int{
    return $a+$b;
}

echo add(1, 2).&#39;<br>&#39;;
echo add(1.5, 2.6);
Copy after login

相关学习推荐:PHP 编程从入门到精通

返回结果:

在php-5.6.27版本及以前版本的返回结果为:

在php-7.0.12版本及以后版本的返回结果为:

php5是无法执行上面代码的,php7执行的时候会先输出一个3和一个报错( Argument 1 passed to add() must be of the type integer, float given);

标量类型声明 有两种模式: 强制 (默认) 和 严格模式。
declare(strict_types=1),必须放在文件的第一行执行代码,当前文件有效!

2.set_exception_handler() 不再保证收到的一定是 Exception 对象

在 PHP 7 中,很多致命错误以及可恢复的致命错误,都被转换为异常来处理了。 这些异常继承自 Error 类,此类实现了 Throwable 接口 (所有异常都实现了这个基础接口)。

PHP7进一步方便开发者处理, 让开发者对程序的掌控能力更强. 因为在默认情况下, Error会直接导致程序中断, 而PHP7则提供捕获并且处理的能力, 让程序继续执行下去, 为程序员提供更灵活的选择。

3.新增操作符“<=>”

代码:

$a=1;$b=1;
$a1=2;$b1=1;
$a2=1;$b2=2;
$c=$a<=>$b;
$c1=$a1<=>$b1;
$c2=$a2<=>$b2;
echo "当\$a=\$b的时候,\$的返回结果为:".$c.&#39;<br>&#39;;
echo "当\$a1>\$b1的时候,\$的返回结果为:".$c1.&#39;<br>&#39;;
echo "当\$a2<\$b2的时候,\$的返回结果为:".$c2.&#39;<br>&#39;;
Copy after login

结果:

在php-5.6.27版本及以前版本的返回结果为:

在php-7.0.12版本及以后版本的返回结果为:

语法:$c = $a <=> $b

如果$a > $b, $c 的值为1

如果$a == $b, $c 的值为0

如果$a < $b, $c 的值为-1

4.新增操作符“??”

如果变量存在且值不为NULL, 它就会返回自身的值,否则返回它的第二个操作数。

代码:

$data[&#39;sex&#39;]=&#39;女&#39;;
$data[&#39;sex2&#39;]=&#39;男&#39;;
//原写法
$sex1 = isset($data[&#39;sex&#39;]) ? $data[&#39;sex&#39;] : &#39;男&#39;;
$sex2 = isset($data[&#39;sex2&#39;]) ? $data[&#39;sex2&#39;] : &#39;男&#39;;
//现在
$sex3 = $data[&#39;sex&#39;] ?? &#39;男&#39;;
$sex4 = $data[&#39;sex2&#39;] ?? &#39;女&#39;;
//$_GET[&#39;sex&#39;]和$_GET[&#39;sex2&#39;]均未传值。
$sex5 = $_GET[&#39;sex&#39;] ?? &#39;男&#39;;
$sex6 = $_GET[&#39;sex2&#39;] ?? &#39;女&#39;;
echo &#39;当获取到的参数为女时原写法返回结果为:&#39;.$sex1."<br>";
echo &#39;当获取到的参数为女时现写法返回结果为:&#39;.$sex3."<br>";
echo &#39;当获取到的参数为男时原写法返回结果为:&#39;.$sex2."<br>";
echo &#39;当获取到的参数为男时现写法返回结果为:&#39;.$sex4."<br>";
echo &#39;当获取到的参数为空时原写法返回结果为:&#39;.$sex5."<br>";
echo &#39;当获取到的参数为空时现写法返回结果为:&#39;.$sex6."<br>";
Copy after login

结果:

在php-5.6.27版本及以前版本的返回结果为:

在php-7.0.12版本及以后版本的返回结果为:

操作符“??”在php7版本就相当于三目运算符的升级版。

5.define() 定义常量数组

代码:

define(&#39;ARR&#39;,[&#39;a&#39;,&#39;b&#39;]);
echo ARR[1];// a
Copy after login

结果:

在php-5.6.27版本及以前版本的返回结果为:

在php-7.0.12版本及以后版本的返回结果为:

6.AST: Abstract Syntax Tree, 抽象语法树

AST在PHP编译过程作为一个中间件的角色, 替换原来直接从解释器吐出opcode的方式, 让解释器(parser)和编译器(compliler)解耦, 可以减少一些Hack代码, 同时, 让实现更容易理解和可维护.

PHP5 : PHP代码 -> Parser语法解析 -> OPCODE -> 执行
PHP7 : PHP代码 -> Parser语法解析 -> AST -> OPCODE -> 执行

7.匿名函数

代码:

$anonymous_func = function(){return &#39;function&#39;;};
echo $anonymous_func(); // 输出function
Copy after login
Copy after login

结果:

在php-5.2.17版本及以前版本的返回结果为:

在php-5.3.29版本及以后版本的返回结果为:

8.Unicode字符格式支持(echo “\u{9999}”)

代码:

$anonymous_func = function(){return &#39;function&#39;;};
echo $anonymous_func(); // 输出function
Copy after login
Copy after login

结果:

在php-5.6.27版本及以前版本的返回结果为:

在php-7.0.12版本及以后版本的返回结果为:

9.Unserialize 提供过滤特性

防止非法数据进行代码注入,提供了更安全的反序列化数据。

10.命名空间引用优化

// PHP7以前语法的写法 
use FooLibrary\Bar\Baz\ClassA; 
use FooLibrary\Bar\Baz\ClassB; 
// PHP7新语法写法 
use FooLibrary\Bar\Baz\{ ClassA, ClassB};
Copy after login

然后再来看下PHP7带来的废弃

1.废弃扩展

Ereg 正则表达式
mssql
mysql
sybase_ct

2.废弃的特性

不能使用同名的构造函数
实例方法不能用静态方法的方式调用

3.废弃的函数

方法调用
call_user_method()
call_user_method_array()

应该采用call_user_func() 和 call_user_func_array()

加密相关函数

mcrypt_generic_end()
mcrypt_ecb()
mcrypt_cbc()
mcrypt_cfb()
mcrypt_ofb()

注意: PHP7.1 以后mcrypt_*序列函数都将被移除。推荐使用:openssl 序列函数

杂项

set_magic_quotes_runtime
set_socket_blocking
Split
imagepsbbox()
imagepsencodefont()
imagepsextendfont()
imagepsfreefont()
imagepsloadfont()
imagepsslantfont()
imagepstext()

4.废弃的用法

$HTTP_RAW_POST_DATA 变量被移除, 使用php://input来代

ini文件里面不再支持#开头的注释, 使用”;”

移除了ASP格式的支持和脚本语法的支持: <% 和 < script language=php >

最后再来看下PHP7带来的变更

1.字符串处理机制修改

含有十六进制字符的字符串不再视为数字, 也不再区别对待.

var_dump("0x123" == "291"); // false
var_dump(is_numeric("0x123")); // false
var_dump("0xe" + "0x1"); // 0
var_dump(substr("f00", "0x1")) // foo
Copy after login

2.整型处理机制修改

Int64支持, 统一不同平台下的整型长度, 字符串和文件上传都支持大于2GB. 64位PHP7字符串长度可以超过2^31次方字节.

// 无效的八进制数字(包含大于7的数字)会报编译错误
$i = 0681; // 老版本php会把无效数字忽略。

// 位移负的位置会产生异常
var_dump(1 >> -1);

// 左位移超出位数则返回0
var_dump(1 << 64);// 0 

// 右位移超出会返回0或者-1
var_dump(100 >> 32);// 0 
var_dump(-100 >> 32);// -1
Copy after login

3.参数处理机制修改

不支持重复参数命名

function func(a,b, c,c) {} ;会报错

func_get_arg()和func_get_args()这两个方法返回参数当前的值, 而不是传入时的值, 当前的值有可能会被修改

所以需要注意,在函数第一行最好就给记录下来,否则后续有修改的话,再读取就不是传进来的初始值了。

function foo($x) {
    $x++;
    echo func_get_arg(0);
}
foo(1); //返回2
Copy after login

4.foreach修改

foreach()循环对数组内部指针不再起作用

$arr = [1,2,3];
foreach ($arr as &$val) {
    echo current($arr);// php7 全返回0
}
Copy after login

按照值进行循环的时候, foreach是对该数组的拷贝操作

$arr = [1,2,3];
foreach ($arr as $val) {
    unset($arr[1]);
}
var_dump($arr);
Copy after login

最新的php7依旧会打印出[1,2,3]。(ps:7.0.0不行)
老的会打印出[1,3]

按照引用进行循环的时候, 对数组的修改会影响循环

$arr = [1];
foreach ($arr as $val) {
    var_dump($val);
    $arr[1]=2;
}
Copy after login

最新的php7依旧会追加新增元素的循环。(ps:7.0.0不行)

5. list修改

不再按照相反的顺序赋值

//$arr将会是[1,2,3]而不是之前的[3,2,1]
list($arr[], $arr[], $arr[]) = [1,2,3];
Copy after login

不再支持字符串拆分功能

// $x = null 并且 $y = null
$str = &#39;xy&#39;;
list($x, $y) = $str;
Copy after login

空的list()赋值不再允许

list() = [123];
Copy after login

list()现在也适用于数组对象

list($a, $b) = (object)new ArrayObject([0, 1]);
Copy after login

6.变量处理机制修改

对变量、属性和方法的间接调用现在将严格遵循从左到右的顺序来解析,而不是之前的混杂着几个特殊案例的情况。 下面这张表说明了这个解析顺序的变化。

Comparison explains the optimization and improvement of PHP7

引用赋值时自动创建的数组元素或者对象属性顺序和以前不同了

$arr = [];
$arr[&#39;a&#39;] = &$arr[&#39;b&#39;];
$arr[&#39;b&#39;] = 1;
// php7: [&#39;a&#39; => 1, &#39;b&#39; => 1]
// php5: [&#39;b&#39; => 1, &#39;a&#39; => 1]
Copy after login

7.杂项

1.debug_zval_dump() 现在打印 “int” 替代 “long”, 打印 “float” 替代 “double”

2.dirname() 增加了可选的第二个参数, depth, 获取当前目录向上 depth 级父目录的名称。

3.getrusage() 现在支持 Windows.mktime() and gmmktime() 函数不再接受 is_dst 参数。

4.preg_replace() 函数不再支持 “\e” (PREG_REPLACE_EVAL). 应当使用 preg_replace_callback() 替代。

5.setlocale() 函数不再接受 category 传入字符串。 应当使用 LC_* 常量。

6.exec(), system() and passthru() 函数对 NULL 增加了保护.

7.shmop_open() 现在返回一个资源而非一个int, 这个资源可以传给shmop_size(), shmop_write(), shmop_read(), shmop_close() 和 shmop_delete().

8.为了避免内存泄露,xml_set_object() 现在在执行结束时需要手动清除 $parse。

9.curl_setopt 设置项CURLOPT_SAFE_UPLOAD变更

TRUE 禁用 @ 前缀在 CURLOPT_POSTFIELDS 中发送文件。 意味着 @ 可以在字段中安全得使用了。 可使用 CURLFile作为上传的代替。 
PHP 5.5.0 中添加,默认值 FALSE。 PHP 5.6.0 改默认值为 TRUE。. PHP 7 删除了此选项, 必须使用 CURLFile interface 来上传文件。

如何充分发挥PHP7的性能

1.开启Opcache

zend_extension=opcache.so 
opcache.enable=1 
opcache.enable_cli=1

2.使用GCC 4.8以上进行编译

只有GCC 4.8以上PHP才会开启Global Register for opline and execute_data支持, 这个会带来5%左右的性能提升(Wordpres的QPS角度衡量)

3.开启HugePage (根据系统内存决定)

Comparison explains the optimization and improvement of PHP7

4.PGO (Profile Guided Optimization)

第一次编译成功后,用项目代码去训练PHP,会产生一些profile信息,最后根据这些信息第二次gcc编译PHP就可以得到量身定做的PHP7

需要选择在你要优化的场景中: 访问量最大的, 耗时最多的, 资源消耗最重的一个页面.

参考: https://blog.csdn.net/weixin_43681537/article/details/84555882
参考: https://blog.csdn.net/weixin_43681537/article/details/84556037

如何更好的写代码来迎接PHP7?

  1. 不使用php7废弃的方法,扩展
  2. 使用2个版本都兼容的语法特性【 list ,foreach, func_get_arg 等】

如何升级当前项目代码来兼容PHP7?

逐步剔除php7不支持的代码

检测工具:https://github.com/sstalle/php7cc

Comparison explains the optimization and improvement of PHP7

The above is the detailed content of Comparison explains the optimization and improvement of PHP7. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install mongo extension in php7.0 How to install mongo extension in php7.0 Nov 21, 2022 am 10:25 AM

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.

How to solve the problem when php7 detects that the tcp port is not working How to solve the problem when php7 detects that the tcp port is not working Mar 22, 2023 am 09:30 AM

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.

What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? Apr 02, 2024 pm 07:39 PM

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.

PHP Server Environment FAQ Guide: Quickly Solve Common Problems PHP Server Environment FAQ Guide: Quickly Solve Common Problems Apr 09, 2024 pm 01:33 PM

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 How to install and deploy php7.0 Nov 30, 2022 am 09:56 AM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

Which one is better, php8 or php7? Which one is better, php8 or php7? Nov 16, 2023 pm 03:09 PM

Compared with PHP7, PHP8 has some advantages and improvements in terms of performance, new features and syntax improvements, type system, error handling and extensions. However, choosing which version to use depends on your specific needs and project circumstances. Detailed introduction: 1. Performance improvement, PHP8 introduces the Just-in-Time (JIT) compiler, which can improve the execution speed of the code; 2. New features and syntax improvements, PHP8 supports the declaration of named parameters and optional parameters, making functions Calling is more flexible; anonymous classes, type declarations of properties, etc. are introduced.

See all articles