Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example introduction and comparison of New Features of php7

2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "example introduction and comparison of the new features of php7". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the example introduction and comparison of the new features of php7".

1. Null merge operator (?)

Syntax: if the variable exists and the value is not NULL, it returns its own value, otherwise it returns its second Operand.

/ / before php7 if judged if (empty ($_ GET ['param'])) {$param = 1;} else {$param = $_ GET [' param'];} / / php7 previous ternary operator $param = empty ($_ GET ['param'])? 1: $_ GET [' param']; / / PHP7 null merge operator $param = $_ GET ['param']? 1

2. Define () defines a constant array

/ / php7 previous define ("CONTENT", "hello world"); echo CONTENT;//hello world / / PHP7 define ('ANIMALS', [' dog', 'cat',' bird']); echo ANIMALS [2]; / / bird / / PHP7 can also be used outside the class to define the constant const CONSTANT = 'Hello World'; echo CONSTANT;//Hello World

3. Combination comparator ()

The combination comparator is used to compare two expressions. It returns-1, 0, or 1 respectively when $an is less than, equal to, or greater than $b. The principle of comparison is based on the conventional comparison rules of PHP.

/ integer echo 1 1; / / 0echo 1 2; / /-1echo 2 1; / / 1 / / floating point echo 1.5 1.5; / / 0echo 1.5 2.5; / /-1echo 2.5 1.5; / / 1 / / string echo "a"a"; / / 0echo "a"b"; / /-1echo "b"a"; / / 1

4. Variable type declaration

There are two modes: mandatory (default) and strict. The following types of parameters are available: string,int,float,bool

/ /... Operator: indicates that this is a variable parameter. Versions of php5.6 and above can be used: functions are defined before variables. Function intSum (int... $ints) {return array_sum ($ints);} var_dump (intSum (2monogramme 3.5')); / 5 / / strict mode / / pattern declaration: declare (strict_types=1); default value is 0, and a value of 1 represents a strictly checked schema declare (strict_types=1); function add (int $return $int $b) {return $astatb;} var_dump (add (2Query 3.5')) / / Fatal error: Uncaught TypeError: Argument 2 passed to add () must be of the type integer

5. Return value type declaration

Added support for return type declarations. Similar to parameter type declaration. (usage adds: type name after the function definition)

/ / valid return type declare (strict_types = 1); function getInt (int $value): int {return $value;} print (getInt (6)); / / 6Accord / invalid return type declare (strict_types = 1); function getNoInt (int $value): int {return $value+'2.5';} print (getNoInt (6)); / / Fatal error: Uncaught TypeError: Return value of getNoInt () must be of the type integer

6. Anonymous class

Allow new class {} to create an anonymous object.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report