"; 3. New operator "??"; 4. Anonymous function; 5. Namespace reference optimization, etc."/> "; 3. New operator "??"; 4. Anonymous function; 5. Namespace reference optimization, etc.">

Home >Backend Development >PHP Tutorial >What are the new features of php7?

What are the new features of php7?

青灯夜游
青灯夜游Original
2019-05-17 18:52:325434browse

What are the new features of php7?

What are the new features of php7? In PHP7, because most of the code of the underlying engine has been modified and the performance of PHP has been improved through various methods, some new syntax has been added to PHP7. The use of these syntax can also help improve performance. Here is a brief introduction to you, I hope it will be helpful to you.

1. Scalar parameter type declaration

You can use string (string), integer (int), floating point number (float), and Boolean value (bool), To declare the parameter type and function return value of the function; previously only two styles of class name, interface, array and Callable were supported: forced conversion mode (default) and strict mode

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 : Enforcement (default) and strict mode.

declare(strict_types=1), must be placed on 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 <=> $b

If $a > $b, $c The value is 1

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

If $a d95faf8f2a25b2266898ac834fbcfce8 Parser syntax Parsing-> OPCODE -> Execute

PHP7: PHP code-> Parser syntax analysis-> AST -> OPCODE -> Execute

7, anonymous function

$anonymous_func = function(){return &#39;function&#39;;};
echo $anonymous_func(); // 输出function

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

9. Unserialize provides filtering features

Prevents code injection of illegal data and provides 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};

The above is the detailed content of What are the new features of php7?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn