Five new features of PHP7-PHP Tutorial

Source: Internet
Author: User
Five new features of PHP7. Five new features of PHP7 1. operator NULL merge operator) put this in the first place because I think it is very useful. Usage: $ a $ _ GET [a]? 1; it is equivalent to five new features of php $ ais PHP 7

1. operator NULL merge operator)

Put this in the first example because I think it is useful. Usage:

$ A = $ _ GET ['A']? 1;

It is equivalent:

$ A = isset ($ _ GET ['A'])? $ _ GET ['A']: 1;

We know that the ternary operator can be used like this:

$? : 1

However, this is based on the premise that $ a has been defined. New ?? Operators can simplify judgment.

2. function return value type declaration

Notes on examples provided in the official documentation... The parameter syntax of the edge length is only available in PHP 5.6 or later versions ):

 
  1.    
  2. Function arraysSum (array... $ arrays): array
  3. {
  4. Return array_map (function (array $ array): int {
  5. Return array_sum ($ array );
  6. }, $ Arrays );
  7. }
  8. Print_r (arraysSum ([1, 2, 3], [4, 5, 6], [7, 8, 9]);

From this example, we can see that all functions, including anonymous functions, can specify the type of the returned value.

The statement is written in a way similar to swift:

 
  1. func sayHello(personName: String) -> String { 
  2. Let greeting = "Hello," + personName + "! "
  3. Return greeting
  4. }

This feature helps us avoid some problems caused by implicit type conversion in PHP. You can think about the expected results before defining a function to avoid unnecessary errors.

However, there is also a feature that requires attention. PHP 7 addsDeclareCommand: strict_types, which uses the strict mode.

When using the return value type declaration, if it is not declared as the strict mode, if the return value is not the expected type, PHP will still forcibly convert it to the type. However, if the mode is strict, a Fatal error of TypeError is triggered.

Force mode:

 
  1.    
  2. Function foo ($ a): int
  3. {
  4. Return $;
  5. }
  6. Fool (1.0 );

The above code can be executed normally. The foo function returns int 1 without any errors.

Strict Mode:

 
  1.    
  2. Declare (strict_types = 1 );
  3. Function foo ($ a): int
  4. {
  5. Return $;
  6. }
  7. Fool (1.0 );
  8. # PHP Fatal error: Uncaught TypeError: Return value of foo () must be of the type integer, float returned in test. php: 6

After the declaration, a fatal error is triggered.

Is it a bit similar to the strict mode of js?

3. scalar type declaration

The parameter type declaration of a function in PHP 7 can be a scalar. In PHP 5, it can only be a class name, interface, array, or callable (PHP 5.4, which can be a function, including an anonymous function ), now you can also use string, int, float, and bool.

Official example:

 
  1.    
  2. // Coercive mode
  3. Function sumOfInts (int... $ ints)
  4. {
  5. Return array_sum ($ ints );
  6. }
  7. Var_dump (sumOfInts (2, '3', 4.1 ));

It should be noted that the strict mode mentioned above is also applicable here: Forced mode default, both forced type conversion) will still force type conversion for parameters that do not meet expectations, in strict mode, the critical error of TypeError is triggered.

4. use batch declaration

In PHP 7, use can declare multiple classes, functions, or const in one sentence:

 
  1.    
  2. Use some/namespace/{ClassA, ClassB, ClassC as C };
  3. Use function some/namespace/{fn_a, fn_ B, fn_c };
  4. Use const some/namespace/{ConstA, ConstB, ConstC };

However, you still need to write out the names of each class, function, or const without the from some import * method like python ).

It should be noted that if you are using a composer-based and PSR-4-based framework, can this method successfully load class files? In fact, it is okay. the automatic loading method registered by composer is to find the location based on the namespace of the class when the class is called. this method has no effect on it.

5. other features

I will not introduce some other features one by one, interested can view the official documentation: http://php.net/manual/en/migration70.new-features.php

Briefly speaking:

  • PHP 5.3 has an anonymous function and now has an anonymous class;

  • Define can now define a constant array;

  • Closure) adds a call method;

  • A generator or iterator is more suitable) you can have a final return value return), or you can use the new syntax of yield from to enter another generator to delegate ).

Two new features of the generator: return and yield from) can be combined. You can test the specific representation on your own. PHP 7 has now reached RC5, and the final version will soon come.

Five new features of http://www.bkjia.com/PHPjc/1064062.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1064062.htmlTechArticlePHP 7 1. operator NULL merge operator) put this in the first place because I think it is useful. Usage: $ a = $ _ GET ['A']? 1; it is equivalent to: php $ a = is...

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.