Home  >  Article  >  Backend Development  >  Comparative explanation of the differences between PHP7 and previous versions

Comparative explanation of the differences between PHP7 and previous versions

coldplay.xixi
coldplay.xixiforward
2021-01-05 09:30:502327browse

PHP7教程栏跟PHP以前版本做的一个对比。

Comparative explanation of the differences between PHP7 and previous versions

推荐(免费):PHP7教程

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

1.类型的声明。

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

代码:

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

echo add(1, 2).'
'; echo add(1.5, 2.6);

返回结果:

在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.'
'; echo "当\$a1>\$b1的时候,\$的返回结果为:".$c1.'
'; echo "当\$a2<\$b2的时候,\$的返回结果为:".$c2.'
';

结果:

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

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

语法:$c = $a $b

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

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

如果$a

4.新增操作符“??”

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

代码:

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

结果:

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

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

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

5.define() 定义常量数组

代码:

define('ARR',['a','b']);
echo ARR[1];// a

结果:

在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 'function';};
echo $anonymous_func(); // 输出function

结果:

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

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

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

代码:

$anonymous_func = function(){return 'function';};
echo $anonymous_func(); // 输出function

结果:

 在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};

然后再来看下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格式的支持和脚本语法的支持:

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

1.字符串处理机制修改

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

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

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

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

4.foreach修改

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

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

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

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

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

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

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

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

5. list修改

不再按照相反的顺序赋值

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

不再支持字符串拆分功能

// $x = null 并且 $y = null
$str = 'xy';
list($x, $y) = $str;

空的list()赋值不再允许

list() = [123];

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

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

6.变量处理机制修改

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

Comparative explanation of the differences between PHP7 and previous versions

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

$arr = [];
$arr['a'] = &$arr['b'];
$arr['b'] = 1;
// php7: ['a' => 1, 'b' => 1]
// php5: ['b' => 1, 'a' => 1]

7.杂项

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

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

3.getrusage() Now supports Windows.mktime() and gmmktime() functions no longer accept the is_dst parameter.

4.preg_replace() The function no longer supports "\e" (PREG_REPLACE_EVAL). Preg_replace_callback() should be used instead.

5.setlocale() The function no longer accepts the category input string. The LC_* constants should be used.

6. The exec(), system() and passthru() functions have added protection against NULL.

7.shmop_open() Now returns a resource instead of a int, this resource can be passed to shmop_size(), shmop_write(), shmop_read(), shmop_close() and shmop_delete().

8. In order to avoid memory leaks, xml_set_object () Now requires manual clearing of $parse at the end of execution.

9.curl_setopt Settings CURLOPT_SAFE_UPLOADChange

TRUE disables the @ prefix for sending files in CURLOPT_POSTFIELDS. This means @ can be safely used in fields. CURLFile can be used as an upload alternative.
Added in PHP 5.5.0, default value is FALSE. PHP 5.6.0 changes the default value to TRUE. . PHP 7 removed this option and you must use the CURLFile interface to upload files.

How to give full play to the performance of PHP7

1. Turn on Opcache

##zend_extension=opcache.so

opcache. enable=1
opcache.enable_cli=1

2. Use GCC 4.8 or above to compile

Only GCC 4.8 or above PHP will open the Global Register for opline and execute_data support, this will bring about 5% performance improvement (measured from the QPS perspective of Wordpres)

3. Turn on HugePage (determined based on system memory)

Comparative explanation of the differences between PHP7 and previous versions

4.PGO (Profile Guided Optimization)

After the first successful compilation, use the project code to train PHP, which will generate some profile information. Finally, based on these Information Compile PHP with gcc for the second time and you can get tailor-made PHP7

You need to choose the scene you want to optimize: the page with the most visits, the most time-consuming, and the heaviest resource consumption.

How to write better code to meet PHP7?

  1. Do not use the abandoned methods of php7 and extend

  2. Use syntax features that are compatible with both versions [list, foreach, func_get_arg, etc.]

How to upgrade the current project code to be compatible with PHP7?

Gradually eliminate codes that are not supported by php7

Detection tool: https://github.com/sstalle/php7cc

The above is the detailed content of Comparative explanation of the differences between PHP7 and previous versions. 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