New features of PHP7

Source: Internet
Author: User
Php7 has been released for half a month. Recently, I have time to understand the new features of php7. of course, the biggest feature of this version is the performance improvement, if you are interested in the related knowledge of the new features of php7, I have been learning about php7 for half a month. Recently, I have some time to learn about the new features of php7. of course, the biggest feature of this version is the performance improvement. I am not a master. You are welcome to point out the mistakes and look forward to sharing them.

A very important feature of the PHP language is the "weak type", which makes PHP programs very easy to write. new users can quickly get started with PHP, but it is also accompanied by some controversy. Variable types are supported, which can be changed by the innovative nature. PHP starts to support type definitions in an optional way. In addition, a switch command declare (strict_type = 1) is introduced. once this command is enabled, the program in the current file will be forced to follow the strict function parameter passing type and return type.

1. added group support for use statements.

Use FooLibrary \ Bar \ Baz \ {ClassA, ClassB, ClassC, ClassD as Fizbo };

2. add ?? Operator

Isset ($ _ GET ['MyKey'])? $ _ GET ['MyKey']: "" bulky $ _ GET ['MyKey']? : "" An E_NOTICE $ _ GET ['MyKey']? will be reported if the mykey does not exist. 'Defaultvalue' security does not report E_NOTICE $ username = $ _ GET ['user']? 'Nobody ';

3. the length of a 64-bit PHP 7 string can exceed 2 ^ 31 power bytes.

4. added Closure: call support

Closure: call dynamically binds a Closure function to a new object instance and calls and executes this function,

 <?php class Value { protected $value; public function __construct($value) { $this->value = $value; } public function getValue() { return $this->value; } } $three = new Value(); $four = new Value(); $closure = function ($delta) { var_dump($this->getValue() + $delta); }; $closure->call($three, ); $closure->call($four, ); ?> // outputs int(),int() 

5. the double quotation mark string and heredocs support the use of \ u {xxxxx} to declare unicode characters.

6. define support for arrays

 define('ANIMALS', array( 'dog', 'cat', 'bird' )); echo ANIMALS[]; // outputs "cat" 

7. added comparison operators <=>

$ A <=> $ B
0 if a is equal to B
1 if a is greater than B
-1 if a is less than B

8. php global reserved words can be declared for use

 class View {  public function include(View $view) {   //...  } } 

The include keyword can be used the same as the common string keyword.

9. supported scalar types (int, float, string, bool)

Add declare (strict_types = 1) command declaration whether strict type verification is performed,
When declare (strict_types = 1) is declared in the file header)

 
 
  

If the preceding code does not enable declare (strict_types = 1) or declare (strict_types = 0), php will automatically convert the parameters and return values to the specified type,
Enable declare (strict_types = 1). If the parameter type is not flaot or the return type is not float, an error is thrown.

10. added interfaces to provide a secure and convenient random number generator for the user layer. RFC:Https://wiki.php.net/rfc/easy_userland_csprng(For further discussion)

11. added the yield from operator.Https://wiki.php.net/rfc/generator-delegation(For further discussion)

Knowledge is what we know and what we know. based on the existing knowledge, we discover the unknown. Therefore, the more knowledge we gain, the more unknown knowledge, there is no end to expanding your knowledge.

Appendix: principles behind PHP7's superior performance

The operating principle of PHP7 is not changed compared with PHP5, which is different from hhvm. The general performance is optimized based on the perf performance analysis tool.

Reduce the number of memory allocations, use the stack memory, cache the array hash value, convert string parsing to macro expansion, and replace small memory with large continuous memory. For more details, see the PHP7 PPT of laruence. The performance of zend Engine programs has improved a lot, and the performance of PHP programs is naturally better. All the optimizations are very real, and the number of details is less than enough.

Trying to optimize a program with a history of 20 years is a very challenging task, and it is more difficult than re-engineering. 80% of the world's websites use PHP. after using PHP 7, these website programs will be able to get doubled performance free of charge. How much CPU will be saved, how much power will be saved, and how much carbon emissions will be reduced. In addition, PHP is an open-source project with no commercial company behind it and is fully operated by the community. Developers and enterprises using the PHP language have never paid a cent to the PHP official website. Therefore, it is recommended that you keep your mind safe, no matter how much you despise PHP, but please respect what others have done for the world.

The above content is a small Editor to introduce you to the new features of PHP7 and the principle behind the excellent performance of PHP7. I hope you will like it.

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.