Home > Article > Backend Development > Let's take a look at the new features of php7
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) ecb704628f0d1ba7498867870ef3bec0 以上程序执行输出结果为: 9复制代码
Strict Mode
43ec82d44a534e8fd5e8416c4719c4dd 以上程序由于采用了严格模式,所以如果参数中出现不适整数的类型会报错,执行输出结果为: PHP Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, called in……复制代码PHP NULL coalescing operator
$site = isset($_GET['site']) ? $_GET['site'] : '菜鸟教程';复制代码
$site = $_GET['site'] ?? '菜鸟教程';复制代码
937b551757918eaa8d4bc2575cf4f5cc复制代码Combined comparison operator, also known as spaceship operator
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 array838d2955445a31c5bbdfb717d04cb05d 1);print(PHP_EOL);print( 1 96b4fef55684b9312718d5de63fb7121 2);print(PHP_EOL);print( 2 96b4fef55684b9312718d5de63fb7121 1);print(PHP_EOL);print(PHP_EOL); // PHP_EOL 为换行符 // 浮点型比较print( 1.5 96b4fef55684b9312718d5de63fb7121 1.5);print(PHP_EOL);print( 1.5 96b4fef55684b9312718d5de63fb7121 2.5);print(PHP_EOL);print( 2.5 96b4fef55684b9312718d5de63fb7121 1.5);print(PHP_EOL);print(PHP_EOL); // 字符串比较print( "a" 96b4fef55684b9312718d5de63fb7121 "a");print(PHP_EOL);print( "a" 96b4fef55684b9312718d5de63fb7121 "b");print(PHP_EOL);print( "b" 96b4fef55684b9312718d5de63fb7121 "a");print(PHP_EOL); ?>复制代码以上结果分别为复制代码0 -1 1 0 -1 1 0 -1 1复制代码
const;
define();
The following is Example:PHP Anonymous Class// 使用 define 函数来定义数组 define('sites', [ 'Google', 'Runoob', 'Taobao']);print(sites[1]); ?> 以上程序执行输出结果为: Runoob复制代码
f59462b884d1e29413c7c5dcd5e665d5logger; } 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("我的第一条日志"); ?> 以上程序执行输出结果为: 我的第一条日志复制代码php Closure::call()
Closure::call() has better performance, dynamically binds a closure function to a new object instance and calls the function.
实例 c887e2e2c47091b63aac6293b2ac837dx; }; // 闭包函数绑定到类 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复制代码PHP filter unserialize()
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() 配合复制代码There are also parameter types
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) |
The above is the detailed content of Let's take a look at the new features of php7. For more information, please follow other related articles on the PHP Chinese website!