New Features of PHP7 and new features of php7
I have sorted out some common new features. Welcome to likes !!!
New operator
1 ,??
$ Username = $ _ GET ['user']? '';
$ Username = isset ($ _ GET ['user'])? $ _ GET ['user']: 'nobody ';
2. <=>
$ Number1 <=> $ number2; when $ number1 is less than, equal to, or greater than $ number2, return-, 1
Add function
Intdiv (divisor, divisor)-integer the division result
Intdiv (3, 2) // 1
Define can define arrays
Define ('animal ',[
'Dog ',
'Cat ',
'Bird'
]);
Return type declaration
Function test (): int
{
Return 1; // true
Return '1'; // true
Return 'string'; // false
}
Scalar type declaration
Function test (string $ name): int
{
Return 22;
}
String integer float boolean
Core errors can be captured.
Error hierarchy
Throwable
Error
ArithmeticError
DivisionByZeroError
AssertionError
ParseError
TypeError
Exception
Optimization of core sorting
Php5: array (1 => 0, 0 => 0) // fast sorting (non-stable sorting)
Php7: array (0 => 0, 1 => 0) // fast sorting + select sorting (stable sorting)
Abstract snytax tree (abstract snytax tree) AST
PHP-> Parser-> AST-> Opcodes-> Execution
Performance is increased, memory consumption is also increased, but can be ignored
An anonymous class can be instantiated through the new class.
Function getAnonymousClass ($ config ){
Return new class ($ config ){};
}
Temporarily bind a method to the object and call
$ F = function (){
P ($ this-> name );
};
Class F {
Private $ name = 'F ';
}
$ F-> call (new F );
Unified syntax Variables
Parentheses do not affect behavior from left to right
$ A = 'B ';
$ B = ['1', '2', '3'];
Var_dump ($ a [1]);
Var_dump ($ a) [1]);
Php5:
Notice: Uninitialized string offset: 1 in E: \ Program Files \ phpStudy \ WWW \ test. php on line 4
Notice: Undefined variable: in E: \ Program Files \ phpStudy \ WWW \ test. php on line 4
NULL
Parse error: syntax error, unexpected '['in E: \ Program Files \ phpStudy \ WWW \ test. php on line 4
Php7: string (1) "2" string (1) "2"
Expression PHP5 PHP7
$ Foo ['bar'] ['baz'] $ {$ foo ['bar'] ['baz']} ($ foo) ['bar'] ['baz']
$ Foo-> $ bar ['baz'] $ foo-> {$ bar ['baz']} ($ foo-> $ bar) ['baz']
$ Foo-> $ bar ['baz'] () $ foo-> {$ bar ['baz']} () ($ foo-> $ bar) ['baz'] ()
Foo: $ bar ['baz'] () Foo ::{ $ bar ['baz']} () (Foo: $ bar) ['baz'] ()
About namespaces
// Pre PHP 7 code
Use some \ namespace \ ClassA;
Use some \ namespace \ ClassB;
Use some \ namespace \ ClassC as C;
Use function some \ namespace \ fn_a;
Use function some \ namespace \ fn_ B;
Use function some \ namespace \ fn_c;
Use const some \ namespace \ ConstA;
Use const some \ namespace \ ConstB;
Use const some \ namespace \ ConstC;
// PHP 7 + code
Use some \ namespace \ {ClassA, ClassB, ClassC as C };
Use function some \ namespace \ {fn_a, fn_ B, fn_c };
Use const some \ namespace \ {ConstA, ConstB, ConstC };
List Modification
1,
List ($ array [], $ array [], $ array []) = [1, 2, 3];
Var_dump ($ array );
Php5: array (3) {[0] => int (3) [1] => int (2) [2] => int (1 )}
Php7: array (3) {[0] => int (1) [1] => int (2) [2] => int (3 )}
2. null values cannot be assigned.
List () = $;
List (,) = $;
List ($ x, list (), $ y) = $;
In php7, Fatal error: Cannot use empty list is reported.
3. String splitting is no longer supported
$ String = "xy ";
List ($ x, $ y) = $ string;
Var_dump ($ x, $ y );
Php5: string (1) "x" string (1) "y"
Php7: null
Foreach Modification
Variable reference will affect the array Loop
$ Array = [0];
Foreach ($ array as & $ val ){
Var_dump ($ val );
$ Array [1] = 1;
}
Php5: int (0)
Php7: int (0) int (1)
Reference Source Address: http://www.php7.site/book/php7/variable-changes-22.html
Improve php performance tips: http://www.laruence.com/2015/12/04/3086.html