严格模式
<?php
declare(strict_types=1);
# 在参数或返回值类型声明前边加上 “?”, 表示返回值要么是 null 要么是声明的类型
function test(?int $a): ?int
{
return $a;
}
var_dump(test(null)); // NULL
var_dump(test(1)); // 1
var_dump(test('a')); // ERROR
常量数组
PHP7 之前是无法通过 define 来定义一个数组常量的,PHP7 支持了这个操作:
<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
throwable 接口
在 PHP7 之前,如果代码中有语法错误,或者 fater error 时,程序会直接报错退出
<?php
try {
undefindfunc();
} catch (Error $e) {
var_dump($e);
}
// or
set_exception_handler(function($e){
var_dump($e);
});
undefindfunc();
list 的方括号写法
$arr = [1, 2, 3];
list($a, $b, $c) = $arr;
[$a, $b, $c] = $arr;
三元运算符
$page = isset($_GET['page']) ?$_GET['page'] : 0;
$page = $_GET['page'] ?? 0;