1. PHP scalar type and return value type declaration
2. PHP NULL coalescing operator
3. PHP spaceship operation operator (combination comparison operator)
4, PHP constant array
5, PHP anonymous class
6, PHP Closure::call()
7 , PHP filter unserialize()
8, PHP IntlChar()
9, PHP CSPRNG
10, PHP 7 exception
11, PHP 7 use Statement
12, PHP 7 error handling
13, PHP intp() function
14, PHP 7 Session options
15, PHP 7 deprecated features
16. Extensions removed in PHP 7
17. SAPI removed in PHP 7
Scalar type declaration
Forced mode
declare(strict_types=1) <?php // 强制模式 function sum(int ...$ints) { return array_sum($ints); } print(sum(2, '3', 4.1)); ?> 以上程序执行输出结果为: 9复制代码
Strict Mode
<?php declare(strict_types=1); function sum(int ...$ints) { return array_sum($ints); } print(sum(2, '3', 4.1)); ?> 以上程序由于采用了严格模式,所以如果参数中出现不适整数的类型会报错,执行输出结果为: PHP Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, called in……复制代码
$site = isset($_GET['site']) ? $_GET['site'] : '菜鸟教程';复制代码
$site = $_GET['site'] ?? '菜鸟教程';复制代码
<?php // 获取 $_GET['site'] 的值,如果不存在返回 '高压锅'$site = $_GET['site'] ?? '高压锅';print($site);print(PHP_EOL); // PHP_EOL 为换行符 // 以上代码等价于$site = isset($_GET['site']) ? $_GET['site'] : '高压锅';print($site);print(PHP_EOL); // ?? 链$site = $_GET['site'] ?? $_POST['site'] ?? '高压锅';print($site); ?>复制代码
PHP 7 newly added spaceship operator ( The combined comparison operator) is used to compare two expressions $a and $b. If $a is less than, equal to, or greater than $b, it returns -1, 0, or 1 respectively.
The following is an examplePHP constant array<?php // 整型比较print( 1 <=> 1);print(PHP_EOL);print( 1 <=> 2);print(PHP_EOL);print( 2 <=> 1);print(PHP_EOL);print(PHP_EOL); // PHP_EOL 为换行符 // 浮点型比较print( 1.5 <=> 1.5);print(PHP_EOL);print( 1.5 <=> 2.5);print(PHP_EOL);print( 2.5 <=> 1.5);print(PHP_EOL);print(PHP_EOL); // 字符串比较print( "a" <=> "a");print(PHP_EOL);print( "a" <=> "b");print(PHP_EOL);print( "b" <=> "a");print(PHP_EOL); ?>复制代码Copy after login以上结果分别为复制代码Copy after login0 -1 1 0 -1 1 0 -1 1复制代码Copy after login
const;
define();
The following is Example:PHP Anonymous Class// 使用 define 函数来定义数组 define('sites', [ 'Google', 'Runoob', 'Taobao']);print(sites[1]); ?> 以上程序执行输出结果为: Runoob复制代码Copy after login
<?php interface Logger { public function log(string $msg); } class Application { private $logger; public function getLogger(): Logger { return $this->logger; } public function setLogger(Logger $logger) { $this->logger = $logger; } } $app = new Application; // 使用 new class 创建匿名类 $app->setLogger(new class implements Logger { public function log(string $msg) { print($msg); } }); $app->getLogger()->log("我的第一条日志"); ?> 以上程序执行输出结果为: 我的第一条日志复制代码
Closure::call() has better performance, dynamically binds a closure function to a new object instance and calls the function.
实例 <?php class A { private $x = 1; } // PHP 7 之前版本定义闭包函数代码 $getXCB = function() { return $this->x; }; // 闭包函数绑定到类 A 上 $getX = $getXCB->bindTo(new A, 'A'); echo $getX(); print(PHP_EOL); // PHP 7+ 代码 $getX = function() { return $this->x; }; echo $getX->call(new A); ?> 以上程序执行输出结果为: 1 1复制代码
unserialize(), It can prevent code injection of illegal data and provide safer deserialized data.
实例 <?php class MyClass1 { public $obj1prop; } class MyClass2 { public $obj2prop; } $obj1 = new MyClass1(); $obj1->obj1prop = 1; $obj2 = new MyClass2(); $obj2->obj2prop = 2; $serializedObj1 = serialize($obj1); $serializedObj2 = serialize($obj2); // 默认行为是接收所有类 // 第二个参数可以忽略 // 如果 allowed_classes 设置为 false, unserialize 会将所有对象转换为 __PHP_Incomplete_Class 对象 $data = unserialize($serializedObj1 , ["allowed_classes" => true]); // 转换所有对象到 __PHP_Incomplete_Class 对象,除了 MyClass1 和 MyClass2 $data2 = unserialize($serializedObj2 , ["allowed_classes" => ["MyClass1", "MyClass2"]]); print($data->obj1prop); print(PHP_EOL); print($data2->obj2prop); ?> 以上程序执行输出结果为: 1 2复制代码
PHP CSPRNG Pseudo-random number generatorNote that the above features areunserialize()
There is an additional parameter selection
allowed_classes
random_bytes() - Cryptographically protected pseudo-random string.
random_int() - Cryptographically protected pseudo-random integer.
rand() and 'mt_rand()'; except that now random_bytes() generates a random string
assert() function. It enables zero-cost assertions in production environments and provides the ability to throw custom exceptions and errors.
assert()的应用 跟assert_option() 配合复制代码
Default value | Optional Values | |
---|---|---|
1 | 1. Generate and execute code (development mode) | 0. Generate code, but skip it during execution -1. Do not generate code (production environment) |
0 | 1. Thrown when the assertion fails, an exception object can be thrown. If no exception is provided, an AssertionError object instance is thrown. | 0 . Use or generate Throwable, just generate warnings based on the object instead of throwing the object (compatible with PHP 5) |