Home  >  Article  >  Backend Development  >  The syntax difference between php5 and php7

The syntax difference between php5 and php7

王林
王林Original
2019-10-17 15:52:545523browse

The syntax difference between php5 and php7

1. Scalar type declaration

The following type parameters can now be used: string (string), integer (int), floating point number ( float), and Boolean (bool). In the old version, function parameter declarations could only be (Array $arr), (CLassName $obj), etc. Basic types such as Int, String, etc. could not be declared.

2. Return value type declaration

PHP 7 adds support for return type declaration. The return type declaration specifies the type of function return value. The available types are the same as The same types are available in the parameter declaration.

3. Null merging operator

There are a lot of cases where ternary expressions and isset() are used simultaneously in the project, and the null merging operator (? ?) This syntactic sugar. If the variable exists and is not NULL, it returns its own value, otherwise it returns the second operand.

旧版:isset($_GET[‘id']) ? $_GET[id] : err;
新版:$_GET['id'] ?? 'err';

4. Combination comparison operator

The combination comparison operator is used to compare two expressions. It returns -1, 0 or 1 when $a is less than, equal to or greater than $b respectively.

5. Anonymous class

Now supports instantiating an anonymous class through new class.

Recommended tutorial: PHP video tutorial

The above is the detailed content of The syntax difference between php5 and 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 [email protected]