Detailed explanation of new features in PHP7.0 (example)
This article is mainly to analyze the new features of php7.0 in detail.
1. Performance and underlying layer
PHP7 is twice as fast as PHP5.6
php7 The most significant changes It is a huge improvement in performance and is close to the PHP execution engine HHVM developed by Facebook. In the WordPress benchmark performance test, the speed is 2 to 3 times faster than version 5.6, which greatly reduces memory usage. PHP7 also has some changes in the language, such as adding return type declarations and adding some new reserved keywords. In terms of security, PHP safe mode is removed, magic quotes are added, etc. Not only that, the new version also supports 64-bit and contains the latest version of the Zend engine.
Test it
A very simple example, generate an array of 600,000 elements, and determine whether the key exists by searching for the key.
1 2 3 4 5 6 7 8 |
|
We tested the performance on php5.6.11 and php7.0.4 respectively.
php5.6.11
1 2 3 4 5 6 |
|
Averaging the three times, the user usage is roughly 0.65 seconds, the system usage is 0.06 seconds, and the CPU rate is 67%. About 1 second in total.
Look at the situation of php7
1 2 3 4 5 6 |
|
In comparison, the user usage time dropped by about 20%, the system usage time dropped by 70%, and the CPU usage rate was as high as 98%. The overall time is reduced to. 0.5 seconds.
Looking at this example, the efficiency is doubled. Not bad indeed.
Look at another example. It also generates an array of 600,000 elements to find whether value exists.
1 2 3 4 5 6 7 8 9 |
|
First look at php5.6.11
1 2 3 4 5 6 7 8 |
|
Then look at php7.0.4
1 2 3 4 5 6 |
|
It is obvious that it is more than 6 times faster. sharp.
2. New features
1. More scalar type declarations
Now there are two modes for scalar in PHP : Enforcement (default) and strict mode. The following type parameters are now available (either in forced or strict mode): string, int, float, and bool. They extend other types introduced in PHP5: class names, interfaces, arrays and callback types. In the old version, function parameter declarations could only be (Array $arr), (CLassName $obj), etc. Basic types such as Int, String, etc. could not be declared.
How to understand? In versions before php7, if we want to limit the type of parameters of a function, there are only two types: array or class.
Before php7:
1 2 3 4 5 6 7 8 9 10 11 |
|
We want to limit the first parameter of getInfo to an array, so we can add an array before the parameter $a. to declare.
Similarly, we want the parameter of getClass to be a class, so we use the className of this class to declare it for mandatory use.
Before php7, only these two scalars could be used.
Let’s use it:
1 2 |
|
We follow the regulations, pass the array as the first parameter, and of course the result will be printed normally:
1 2 3 4 5 6 7 8 9 10 |
|
If we don’t install the regulations Come, a well-known error will be reported:
1 2 |
|
Error report:
1 2 3 4 |
|
The same is true for using the class:
1 2 |
|
Output result:
1 2 |
|
Similarly, we pass If you enter other parameters, an error will be reported:
1 2 3 4 5 6 |
|
Let’s go back to the php7 upgrade, which expanded the scalar types and added bool, int, string, and float.
php7 has 2 two modes: mandatory (default) and strict mode.
Forced mode
Forced mode is the default mode. In forced mode, it will help us convert the numeric type string type, int integer type, bool, and forced type. Other types cannot be converted and an error will be reported.
Still the example just now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Let’s first pass in all int 1
1 2 3 4 5 |
|
and look at the printed result, it has already been forced to convert for us.
1 2 3 4 5 6 7 8 9 |
|
Let’s continue and pass in string 1.23:
1 2 3 4 5 |
|
Take a look and print the result. It has also been forced to convert for us.
1 2 3 4 5 6 7 8 9 |
|
But if the parameter is an array, we cannot force the conversion, and an error will be reported:
1 2 3 |
|
Will we report an error when we run these codes in PHP5.6.11? Give it a try:
1 2 3 4 5 6 7 |
|
Okay. An error was reported directly. Although the error message also said that the type was wrong, other declarations of these types were not supported.
strict mode
As mentioned before, in forced mode, it will help us force conversion, so what about strict mode?
First of all, how to turn on strict mode?
1 2 |
|
Just add it. In this way, you will enter strict mode. The parameters must comply with the regulations, otherwise an error will be reported:
Let’s add this sentence and run it again:
1 2 3 4 5 6 7 8 9 |
|
Run it and look at the results. Sure enough, an error is reported directly.
1 |
|
2. Return value type declaration
We know that PHP functions do not have return value types. Whatever type is returned is the type. The return value type has been added in php7, and we can define the return value type of a function.
Same as the scalar type declaration in PHP7 upgrade, the return type can be the following: bool, int, string, float, array, class.
For example, if we want the return value of a function to be an array, we can write it like this:
: array {} // colon + return type
1 2 3 4 |
|
Don’t you think it’s strange and incredible! ! !
View the print results:
1 2 3 4 5 6 7 8 9 10 |
|
Similarly, we want to return an integer type:
1 2 3 4 |
|
查看结果,他已经帮我们强制转换成整型了。
1 2 3 |
|
同样,我们可以返回一个class类型的:
1 2 3 |
|
默认,也是强制模式,会帮我们转换,如果,我们想使用严格模式,同样是一样的,在文件头部加上:
1 2 |
|
就可以了,这样,我们规定返回值是什么类型,就必须得是这样,不然就报致命报错。
3. null合并运算符 (??)
由于日常使用中存在大量同时使用三元表达式和 isset()的情况, php7增加了一个新的语法糖 : null合并运算符 (??)
如果变量存在且值不为NULL, 它就会返回自身的值,否则返回它的第二个操作数。
1 2 3 4 |
|
确实方便了很多。
我记得php5.3的更新中,加入了 三元运算符简写形式:
1 |
|
千万别和??搞混淆了!!!
$a ?: $b的意思是 $a为true时,直接返回$a, 否则返回$b
$a ?? $b的意思是 $a isset($a)为true, 且不为NULL, 就返回$a, 否则返回$b。
看例子:
1 2 3 4 5 |
|
4. 太空船操作符(组合比较符)
php7 中,新加入了一个比较符号:<=> ,因为长相像太空船,所以,也叫太空船操作符。
它有啥用呢?
<=>用于比较两个表达式。当$a小于、等于或大于$b时它分别返回-1、0或1。
看例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
其实,蛮多地方可以派上用场的。
5. 通过define()定义常量数组
Array类型的常量现在可以通过 define()来定义。在 PHP5.6 中仅能通过const定义。
在php5.3中,增加了可以使用const来申明常量,替代define()函数,但是只能申明一些简单的变量。
1 2 3 4 5 6 7 8 9 |
|
在php5.6中,又对const进行来升级,可以支持上面的运算了。
1 2 |
|
但是,一只都是在优化const,可是确把define()给搞忘记了,php 5.6申明一个数组常量,只能用const。所以,在 php7 中把 define()申明一个数组也给加上去了。
1 2 3 4 5 6 |
|
至此,到php7版本,define()的功能和const就一摸一样了,所以,你随便用哪一个都可以,但是因为在class类中,什么常量是const。所以,我们就统一用const申明常量好了。
6. 匿名类
现在已经支持通过new class 来实例化一个匿名类,这可以用来替代一些用后即焚的完整类定义。
看下这个官方文档上的一个栗子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
我们先输出的打印的结果,显示为匿名类:
1 2 |
|
我们来分解下,还原被偷懒的少写的代码:
1 2 3 4 5 6 7 8 |
|
输出结果为:
1 2 |
|
虽然代码简洁了很多,但是还是有点不适应,多用用就好了。
还记得php中的匿名函数嘛?在php5.3中新增的匿名函数,结合新的,顺便复习下:
1 2 3 4 5 6 |
|
输出结果为:
1 2 3 4 5 6 |
|
7. Unicode codepoint 转译语法
ps : 由于用的少,我就直接抄官网的说明了。
这接受一个以16进制形式的 Unicode codepoint,并打印出一个双引号或heredoc包围的 UTF-8 编码格式的字符串。 可以接受任何有效的 codepoint,并且开头的 0 是可以省略的。
1 2 3 |
|
看下输出:
1 |
|
我们在php5.6环境下执行下呢?会怎样:
1 |
|
好吧,直接原样输出了。
8. Closure::call() 闭包
ps : 由于用的少,我就直接抄官网的说明了。
Closure::call() 现在有着更好的性能,简短干练的暂时绑定一个方法到对象上闭包并调用它。
1 2 3 4 5 6 7 8 9 |
|
会输出:
1 2 |
|
9. 为unserialize()提供过滤
unserialize 这个函数应该不陌生,它是php中用解开用serialize序列化的变量。
看个栗子:
1 2 3 4 5 |
|
打印结果为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
现在php7中unserialize会变得更佳好用,它多了一个参数,用来反序列化包涵class的过滤不需要的类,变的更加安全。
1 2 3 |
|
举个例子,先序列化一个类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
发现3和5直接报致命错误了:
1 |
|
大致意思就是,没权限解析。
所以,我们改一下:
1 2 |
|
再输出,就正常了。
1 2 3 4 5 6 7 8 9 10 |
|
发现我目前为止并没用到,并没有什么乱用,好吧,继续下一个。
10. IntlChar
ps : 由于用的少,我就直接抄官网的说明了。
新增加的 IntlChar(http://php.net/manual/zh/class.intlchar.php) 类旨在暴露出更多的 ICU 功能。这个类自身定义了许多静态方法用于操作多字符集的 unicode 字符。
1 2 3 4 |
|
以上例程会输出:
1 2 3 |
|
若要使用此类,请先安装Intl扩展
相关推荐:《PHP7新特性手册》
The above is the detailed content of Detailed explanation of new features in PHP7.0 (example). 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

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

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
