The following are some additions to the PHP 7.0 & 7.1 version, respectively.
PHP 7.0
?? Operator
$foo = null; $bar = $foo?? 123;//equivalent to $bar = Isset ($bar)? $bar: 123
Parameter type, return type, and strict mode
In strict mode, the parameter type mismatch throws an error declare (Strict_types=1); function fn (int $a): int{ echo $a;} FN (1.2);
Combining a comparison character
Returns 0 when they are equal;
The former is greater than the latter return 1;
The latter is greater than the former return-1;
Var_dump ($a <=> $b);
$a = 0; $b = "abc"; var_dump ($a <=> $b);//Note that strings are treated as 0 when compared to numbers;
Generator with return value
function Generator () { yield 1; Yield 2; Return "a";} $generator = Generator (), foreach ($generator as $val) { var_dump ($val);} Var_dump ($generator->getreturn ());
When used without a return value $generator->getReturn() , it is returned null . Also, when the generator does not have the output complete, the use $generator->getReturn() will error.
function Generator () { yield 1; Yield 2; Return "a";} $generator = Generator (), Var_dump ($generator->current ()), Var_dump ($generator->next ()), Var_dump ($generator- >getreturn ()); Error Var_dump ($generator->current ()); Var_dump ($generator->next ());
Defining a constant array
Define (' COLORS ', [' Red ', ' blue ', ' black ']); Echo colors[1]; Red
Multiple use declarations
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};
Array Deconstruction
List ($a, $b) = ["A", "B"];
PHP 7.1
Jit
JIT, just in time. The runtime converts part of the instruction to machine code. There is a high performance boost for compute-intensive applications.
Optional parameter types
function (? string $name) { var_dump ($name);} The argument type is string or null
Note the difference from the default parameter values:
function (String $name = "Default-name") { var_dump ($name);}
Array Deconstruction
[$a, $b] = ["A", "B"];
Iterable & Callable Pseudo-class types
Use the callable type to represent the type of the argument that is callable (function, class instance that implements Invoke);
Use the iterable type to restrict the type of the argument to an iterative type (array, class instance that implements the Iterator or traversable interface);
Multiple exception type capture
try { } catch (Oneexception | Anotherexception $e) { }