Summary of new functions and grammatical changes of PHP7

Source: Internet
Author: User

1. Scalar type declaration
There are two modes: mandatory (default) and strict mode. You can now use the following type parameters, whether in forced or strict mode: string (String), Integer (int), floating-point number (float), and Boolean (bool).
2. return value type declaration
The return type declaration indicates the type of the function return value. The available types are the same as the types available in the parameter declaration.

<? PHP function arrayssum (array []):array{returnarray_map(  function(array$array): int {returnarray_sum($array );} $arrays );} Print_r ([Arrayssum], [4,5,6], [7,8,9]); >

3, NULL merge operator

There is a large number of simultaneous use of ternary and isset () in the project, the addition of the null merge operator (??) This syntax (if the variable exists and the value is not NULL, it returns its own value, otherwise it returns the second operand.) )
4. Spaceship operator (combination comparator)
The spaceship operator is used to compare two expressions. When $ A is less than, equal to, or greater than $b, it returns-1, 0, or 1, respectively

<?PHP//integersEcho1 <=> 1;//0Echo1 <=> 2;//-1Echo2 <=> 1;//1//FloatsEcho1.5 <=> 1.5;//0Echo1.5 <=> 2.5;//-1Echo2.5 <=> 1.5;//1//StringsEcho"A" <=> "a";//0Echo"A" <=> "B";//-1Echo"B" <=> "a";//1?>

5. Define a constant array by define ()

<? PHP Define (' ANIMALS ', [' dog ', ' cat ', ' bird '); Echo // outputs "Cat"?>

6. Anonymous class

It is now supported to instantiate an anonymous class with the new class

<?PHPInterfaceLogger { Public function Log(string $msg);}classApplication {Private $logger; Public functionGetLogger ():Logger {return $this-logger;} Public functionSetlogger (Logger$logger) {$this->logger =$logger;}}$app=NewApplication;$app->setlogger (New class ImplementsLogger { Public function Log(string $msg) {Echo $msg;}});Var_dump($app-GetLogger ());?>

7. Unicode codepoint Translation Syntax

This accepts a Unicode codepoint in the form of 16, and prints a string in a double-quote or heredoc-enclosed UTF-8 encoded format. Any valid codepoint can be accepted, and 0 of the beginning can be omitted.

...... Cond

Summary of new functions and grammatical changes 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.