New features of PHP7

Source: Internet
Author: User
Tags null null parse error

New operator
1 、??
$username = $_get[' user ']?? ‘‘;
$username = isset ($_get[' user ')? $_get[' user ']: ' nobody ';

2. <=>
$number 1 <=> $number 2; Returns -1,0,1 when $number 1 is less than, equal to, greater than $number 2 o'clock


New function
Intdiv (dividend, divisor)-Rounding the result of the division
Intdiv (3, 2)//1



Define can define an array
Define (' ANIMALS ', [
' 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;
}
String Integer Float boolean


can catch core errors
Error hierarchy
Throwable
Error
Arithmeticerror
Divisionbyzeroerror

Assertio Nerror
ParseError
TypeError

Exception


Core Sort optimization
Php5:array (1=>0, 0=>0)//Quick sort (unstable Ordered)
Php7:array (0=>0, 1=>0)//Quick sort + Select sort (Stable sort)



More abstract syntax than PHP5 (abstract snytax tree) AST
Parser, AST->opcodes, PHP, execution
Performance increased, memory consumption increased but negligible


Anonymous classes can instantiate an anonymous class with new class
Function Getanonymousclass ($config) {
Return new Class ($config) {};
}



Temporarily binds a method to the object and calls
$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 ' [' 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};


Changes to List
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, do not allow the assignment of null values
List () = $a;
List (,,) = $a;
List ($x, List (), $y) = $a;

PHP7 will error Fatal error:cannot use empty list

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 NULL

Modification of foreach
Variable references that affect the loop of an array
$array = [0];
foreach ($array as & $val) {
Var_dump ($val);
$array [1] = 1;
}
Php5:int (0)
Php7:int (0) int (1)

New features of PHP7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.