Five new features of PHP 7-Alibaba Cloud Developer Community

1. Operator (NULL merge operator)

I put this in the first place because I think it is very useful. Usage:

$a = $_GET['a'] ?? 1;

it is equivalent:

<php $a = isset($_GET['a']) ? $_GET['a'] : 1;

we know that the ternary operator can be used as follows:

$a? : 1

however, this is based on the premise that $a has already been defined. Added?? Operators simplify judgment.

2. Declaration of function return value type

examples provided in official documents (note... The edge length parameter syntax is only available in PHP 5.6 and later versions):

  1. <php
  2. function arraysSum ( array ... $arrays ): array  
  3. {
  4.     return  array_map ( function ( array  $array ): int {
  5.         return  array_sum ( $array );
  6. }, $arrays );
  7. }
  9. print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

as shown in this example, functions (including anonymous functions) can now specify the type of the returned value.

This statement is somewhat similar to swift:

  1. func sayHello(personName: String) -&gt; String {
  2. let greeting = "Hello, " + personName + "! "  
  3.     return greeting
  4. }

this feature can help us avoid some problems caused by implicit type conversion in PHP. Considering the expected results before defining a function can avoid some unnecessary errors.

However, there is also a feature that needs attention. PHP 7 adds one declare command: strict_types, use both strict mode.

If the return value type is not declared in strict mode, PHP will force the type conversion if the return value is not the expected type. However, if it is a strict mode, a TypeError Fatal error will be set out.

Force mode:

  1. <php
  2. function foo ( $a ) : int
  3. {
  4.     return  $a ;
  5. }
  7. foo(1.0);

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

Strict Mode:

  1. <php
  2. declare (strict_types = 1);
  4. function foo ( $a ) : int
  5. {
  6.     return  $a ;
  7. }
  9. foo(1.0);
  10. # 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 similar to the strict mode of js?

3. Scalar type declaration

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

Official example:

  1. <php
  2. // Coercive mode  
  3. function sumOfInts(int... $ints )
  4. {
  5.     return  array_sum ( $ints );
  6. }
  8. var_dump(sumOfInts(2 '3' , 4.1);

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

4. use batch declaration

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

  1. <php
  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, the name of each class or function or const must be written (there is no method from some import * like python).

The question to be noted is: if you are using a composer and PSR-4-based framework, can this writing method successfully load class files? In fact, the automatic loading method registered by composer is to find the location according to the namespace of the class when the class is called. This writing method does not affect it.

5. Other features

I will not introduce other features one by one. If you are interested, please see the official document:


  • PHP 5.3 started with anonymous functions, and now has Anonymous classes;

  • define: you can now define a constant array;

  • closure (Closure) added a call method;

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

Two new features of the generator (return and yield from) can be combined. You can test the specific appearance by yourself. PHP 7 is now RC5, and the final version should come soon.

Source: 51CTO

Please read this disclaimer carefully before you start to use the service. By using the service, you acknowledge that you have agreed to and accepted the content of this disclaimer in full. You may choose not to use the service if you do not agree to this disclaimer. This document is automatically generated based on public content on the Internet captured by Machine Learning Platform for AI. The copyright of the information in this document, such as web pages, images, and data, belongs to their respective author and publisher. Such automatically generated content does not reflect the views or opinions of Alibaba Cloud. It is your responsibility to determine the legality, accuracy, authenticity, practicality, and completeness of the content. We recommend that you consult a professional if you have any doubt in this regard. Alibaba Cloud accepts no responsibility for any consequences on account of your use of the content without verification. If you have feedback or you find that this document uses some content in which you have rights and interests, please contact us through this link: We will handle the matter according to relevant regulations.
Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now