Home  >  Article  >  Backend Development  >  New features of php7: New things brought by php7

New features of php7: New things brought by php7

L
Lforward
2020-05-30 09:56:222632browse

New features of php7: New things brought by php7

New things brought by PHP7

1. Type declaration.

You can use strings (string), integers (int), floating point numbers (float), and Boolean values ​​(bool) to declare the function parameter types and function returns value.

declare(strict_types=1);
function add(int $a, int $b): int {
return $a+$b;
}
echo add(1, 2);
echo add(1.5, 2.6);

php5 cannot execute the above code. When php7 is executed, it will first output a 3 and an error (Argument 1 passed to add() must be of the type integer, float given);

There are two modes for scalar type declarations: mandatory (default) and strict mode.
declare(strict_types=1), must be placed in the first line of the file to execute the code, the current file is valid!

2.set_exception_handler() no longer guarantees that what is received must be an Exception object

In PHP 7, there are many fatal errors and recoverable fatal errors , are converted into exceptions for processing. These exceptions inherit from the Error class, which implements the Throwable interface (all exceptions implement this base interface).

PHP7 further facilitates developers' processing and allows developers to have greater control over the program. Because by default, Error will directly cause the program to interrupt, while PHP7 provides the ability to capture and process it, allowing the program to continue. The implementation continues to provide programmers with more flexible options.

3. New operator "96b4fef55684b9312718d5de63fb7121"

Syntax: $c = $a 96b4fef55684b9312718d5de63fb7121 $b

If $a > $b, the value of $c is 1

If $a == $b, the value of $c is 0

If $a 37efa77f5e5c4507d5a08145cb5b4f37 Parser syntax analysis-> OPCODE -> Execution
PHP7: PHP code-> Parser syntax analysis-> AST -> OPCODE -> Execution

Reference: https://wiki.php.net/rfc/abstract_syntax_tree

7. Anonymous function

$anonymous_func = function(){return 'function';};
echo $anonymous_func(); // 输出function

8.Unicode character format support (echo “\u{9999}”)

9.Unserialize provides filtering features

Prevent code injection of illegal data and provide safer deserialized data.

10. Namespace reference optimization

// PHP7以前语法的写法
use FooLibrary\Bar\Baz\ClassA;
use FooLibrary\Bar\Baz\ClassB;
// PHP7新语法写法
use FooLibrary\Bar\Baz\{ ClassA, ClassB};

Recommended tutorial: "PHP7 Tutorial"

The above is the detailed content of New features of php7: New things brought by php7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete