search
HomeBackend DevelopmentPHP7New features of php7: New things brought by php7

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 ""

Syntax: $c = $a $b

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

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

If $a

4. New operator "??"

If the variable exists and the value is not NULL, it will return its own value. Otherwise its second operand is returned.

//原写法
$username = isset($_GET['user]) ? $_GET['user] : 'nobody';
//现在
$username = $_GET['user'] ?? 'nobody';

5.define() Define constant array

define('ARR',['a','b']);
echo ARR[1];// a

6.AST: Abstract Syntax Tree, abstract syntax tree

AST plays the role of a middleware in the PHP compilation process, replacing the original method of spitting out opcode directly from the interpreter, decoupling the interpreter (parser) and the compiler (compliler), which can reduce some Hack codes and make the implementation more efficient. Easy to understand and maintainable.

PHP5: PHP code-> 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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor