Home > Article > Backend Development > Let’s take a look at the performance upgrades brought by php7
Preface
This article is a summary of a lecture + follow-up research.
Speaking of following the fashion back then, I immediately installed php7 on my computer as soon as it came out. php5 and php7 coexisted. I immediately wrote a super time-consuming loop script and tested it. It is true that php7 is much more powerful, and then I paid some attention to it. New features and some discarded usage.
Since php upgrade is a top priority, the company only plans to upgrade in the near future, so I have been able to appreciate the pleasure brought by php7 in private. The friend in charge of the upgrade made a sharing, which is quite comprehensive. Mark it here. Take notes.
Related free learning recommendations: WeChat applet development
Main research questions:
1.PHP7 with Benefits
2. New things brought by PHP7
3. Obsolescence brought by PHP7
4. Changes brought by PHP7
5. How to give full play to the performance of PHP7
6. How to write better code to welcome PHP7?
7. How to upgrade the current project code to be compatible with PHP7?
Benefits of PHP7
Yes, in terms of performance A substantial improvement can save machines and save money.
New things brought by PHP7
1. Type declaration
You can use string (string), integer (int) , floating point number (float), and Boolean value (bool) to declare the parameter type and function return value of the function.
declare(strict_types=1); function add(int $a, int $b): int { return $a+$b;} echo add(1, 2); echo add(1.5, 2.6);
php5 cannot execute the above code. When php7 is executed, it will first output a 3 and an error (Argument 1 passed to add() must be of the type integer, float given);
There are two modes for scalar type declarations: mandatory (default) and strict mode.
declare(strict_types=1), must be placed in the first line of the file to execute the code, the current file is valid!
2.set_exception_handler() no longer guarantees that what is received must be an Exception object
In PHP 7, many fatal errors and recoverable fatal errors are converted into exceptions Come to deal with it. These exceptions inherit from the Error class, which implements the Throwable interface (all exceptions implement this base interface).
PHP7 further facilitates developers' processing and allows developers to have greater control over the program. Because by default, Error will directly cause the program to interrupt, while PHP7 provides the ability to capture and process it, allowing the program to continue. The implementation continues to provide programmers with more flexible options.
3. New operator "96b4fef55684b9312718d5de63fb7121"
Syntax: $c = $a 96b4fef55684b9312718d5de63fb7121 $b
If $a > The value of $b, $c is 1
If $a == $b, the value of $c is 0
If $a a7ffa1d5d2ae93251f886be5f9399ec8 Parser syntax analysis-> OPCODE -> Execution
PHP7: PHP code-> Parser syntax analysis-> AST -> OPCODE -> ; Execution
7.Anonymous function
$anonymous_func = function(){return 'function';}; echo $anonymous_func(); // 输出function
8.Unicode character format support (echo “\u{9999}”)
9.Unserialize provides filtering features
to prevent code injection of illegal data and provides safer deserialized data.
10. Namespace reference optimization
// PHP7以前语法的写法 use FooLibrary\Bar\Baz\ClassA; use FooLibrary\Bar\Baz\ClassB; // PHP7新语法写法 use FooLibrary\Bar\Baz\{ ClassA, ClassB};
Abandoned by PHP7
1. Deprecated extension
Ereg regular expression
mssql
mysql
sybase_ct
2. Deprecated features
cannot use the same name The constructor
instance method cannot be called as a static method
3. The obsolete function
method call
call_user_method() call_user_method_array()
should use call_user_func () and call_user_func_array()
Encryption related functions
mcrypt_generic_end() mcrypt_ecb() mcrypt_cbc() mcrypt_cfb() mcrypt_ofb()
Note: mcrypt_* sequence functions will be removed after PHP7.1. Recommended use: openssl sequence function
Miscellaneous
set_magic_quotes_runtime set_socket_blocking Split imagepsbbox() imagepsencodefont() imagepsextendfont() imagepsfreefont() imagepsloadfont() imagepsslantfont() imagepstext()
4. Obsolete usage
$HTTP_RAW_POST_DATA variable has been removed, use php://input to replace
Comments starting with # are no longer supported in ini files, use ";"
Removed ASP format support and script syntax support: 901dcc52d4ee3bf734729d92a0a0dcb8
##Changes brought by PHP7
1. String processing mechanism modificationcontains Strings of hexadecimal characters are no longer regarded as numbers, and are no longer treated differently.
var_dump("0x123" == "291"); // false var_dump(is_numeric("0x123")); // false var_dump("0xe" + "0x1"); // 0 var_dump(substr("f00", "0x1")) // foo
2. Integer processing mechanism modificationInt64 support, unified under different platforms The integer length, string and file upload all support greater than 2GB. The 64-bit PHP7 string length can exceed 2^31 bytes.
// 无效的八进制数字(包含大于7的数字)会报编译错误$i = 0681; // 老版本php会把无效数字忽略。 // 位移负的位置会产生异常var_dump(1 >> -1); // 左位移超出位数则返回0var_dump(1 << 64);// 0 // 右位移超出会返回0或者-1var_dump(100 >> 32);// 0 var_dump(-100 >> 32);// -1
3. Parameter processing mechanism modification Does not support repeated parameter naming
function func(a,a,b, c,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.变量处理机制修改
对变量、属性和方法的间接调用现在将严格遵循从左到右的顺序来解析,而不是之前的混杂着几个特殊案例的情况。 下面这张表说明了这个解析顺序的变化。
引用赋值时自动创建的数组元素或者对象属性顺序和以前不同了
$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() 现在支持 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 (根据系统内存决定)
4.PGO (Profile Guided Optimization)
第一次编译成功后,用项目代码去训练PHP,会产生一些profile信息,最后根据这些信息第二次gcc编译PHP就可以得到量身定做的PHP7
需要选择在你要优化的场景中: 访问量最大的, 耗时最多的, 资源消耗最重的一个页面.
相关学习推荐:小程序开发教程
The above is the detailed content of Let’s take a look at the performance upgrades brought by php7. For more information, please follow other related articles on the PHP Chinese website!