PHP7除了在性能方面提升了很多,也加入了很多新特性,一起看一下吧。
1、太空船操作符(<=>)
太空船操作符用于比较两个表达式,大于、等于和小于时分别返回1、0、-1。1
2
3
4
5
6
7
8
echo 1 <=> 1; //0
echo 1 <=> 0; //1
echo 1.8 <=> 2; //-1
echo 'a' <=> 'a'; //0
echo 'a' <=> 'b'; //-1
echo 'b' <=> 'a'; //1
2、标量类型申明和返回值类型声明
PHP7可以对以下几种标量类型的参数做声明:字符串(string)、整型(int)、浮点型(float),布尔型(bool)。参数类型声明不受限于默认模式和严格模式,默认模式时传入的参数不符合声明类型会进行类型转换,严格模式则会报错。参数类型声明1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//声明为严格模式,注释此行将不会报错
declare(strict_types = 1);
function sum(int ...$ints)
{
return array_sum($ints);
}
var_dump(sum(1, '1.1', 1.1));
/*执行结果如下:
declare(strict_types = 1);
PHP Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given
declare(strict_types = 1);
int(3)
*/
返回值类型声明1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//声明为严格模式,注释此行将不会报错
declare(strict_types = 1);
function sum(int ...$ints) : int
{
return array_sum($ints);
}
var_dump(sum(1, '1.1', 1.1));
/*执行结果如下:
declare(strict_types = 1);
PHP Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given
declare(strict_types = 0);
int(3)
*/
PHP7.1可以将函数返回值类型定义为 viod ,无论是否开启严格模式,只要函数中有 return;,那么其他 return语句都将报错。参数类型不可以声明为 viod。返回值声明为 void1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
declare(strict_types = 1);
function testVoid(int ...$ints) : void
{
return array_sum($ints);
return null;
return ;
}
var_dump(testVoid(1, 2, 6));
/*运行结果如下:
return array_sum($ints);
PHP Fatal error: A void function must not return a value
return null;
PHP Fatal error: A void function must not return a value (did you mean "return;" instead of "return null;"?)
return;
NULL
*/
PHP7.1对于参数类型和返回值类型还可以是可空类型,在参数或者返回值类型声明前加?即可。可空参数类型和返回值类型1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
declare(strict_types = 1);
function test(?int ...$ints) : ?int
{
return array_sum($ints);
return strval(implode(',', $ints));
return null;
return;
}
var_dump(test(1, 2, 4));
/*执行结果如下:
return array_sum($ints);
int(7)
return strval(implode(',', $ints));
PHP Fatal error: Uncaught TypeError: Return value of test() must be of the type integer or null, string returned
return null;
NULL
return;
PHP Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?)
3、null合并运算符
PHP 7以前我们通常会写这样的代码:
$name = isset($res['name']) ? $res['name'] : '';
PHP7以后我们可以这样写:
$name = $res['name'] ?? '';
这样就极大简化了我们的代码。
对于这样的代码我们也可以做简化:
$name = !empty($res['name']) ? $res['name'] : '';
$name = $res['name'] ?: '';
对于连续的三元运算符还可以这样写:
$name = $res['name'] ?? $data['name'] ?? '';4、define定义常量数组
PHP7之前无法通过define来定义一个常量数组,PHP7支持。1
2
3
4
5
6
7
8
9
10
11
12
13
define('TYPES', [
1, 2, 3
]);
print_r(TYPES);
/*执行结果如下:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
5、批量导入namespace命名空间
PHP7之前如果需要导入同一个namespace的多个类,需要这样写:1
2
3
4
use Namespace\A;
use Namespace\B;
use Namespace\C;
PHP7可以这样写:1
2
use Namespace\{A, B, C};
6、throwable接口
PHP7之前如果代码有语法错误,或者fatal error程序会直接报错退出,PHP7实现了throwable接口,原来的Exception和部分Error实现了该接口,那么这种Error和Exception会被第一个try catch捕获。没有匹配的catch块,则会调用异常处理函数,如果没有注册,则会fatal error。Error类并非继承Exception,所以要想捕获Error,可以catch (Error $e) {}或者通过注册异常处理函数set_exception_handler()来捕获Error。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
try {
test();
} catch (Error $e) {
print_r($e);
}
set_exception_handler(function ($e) {
print_r($e);
});
test();
/*执行结果如下:
Error Object
(
[message:protected] => Call to undefined function test()
[string:Error:private] =>
[code:protected] => 0
[file:protected] => D:\xampp\htdocs\demo\bz.github.io\exception.php
[line:protected] => 14
[trace:Error:private] => Array
(
)
[previous:Error:private] =>
[xdebug_message] =>
Error: Call to undefined function test()
)
*/
7、intdiv函数1
2
3
echo intdiv(10, 3); // 3
echo intval(10 / 3); // 3