PHP7.0 and 7.1 Parts new features memo code sharing

Source: Internet
Author: User

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) {    }

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.