We should learn new features in PHP 7.

Source: Internet
Author: User
Tags php operator
Some new features of php7 are how to use PHP7 to bring significant performance improvements and new features, and to improve some of the features of previous versions. This article will help you understand the new features in PHP 7.

1. scalar type declaration

We know that PHP is a weak type programming language, so no method is provided to specify the type of input parameters and return values. PHP7 breaks through this situation and adds the scalar type (int, float, string, bool) declaration support, added declare (strict_types = 1) command declaration whether strict type verification, let's look at a piece of code:

declare(strict_types=1)function add(int $x, int $y) : int{    return $x + $y;}echo add(1, 2);  //int(7)declare(strict_types=1)function add(int $x, int $y) : int{    return $x + $y;}echo add(1, 2);  //int(7)

Valid types include class/interface name, self, array, callable, bool, float, int, and string.

2. NULL merge operator

The NULL merge operator is added to PHP 7. do not underestimate this "?", With it, we can easily obtain a parameter and provide a default value when it is null. How ?? If the left value of the operator exists and is not NULL, the left value is returned. Otherwise, the right value is returned. Let's try the following code ?? Powerful operators.

 
 

3. anonymous class

As the name implies, there is no class name, and its declaration and instantiation are at the same time. PHP7 supports instantiating an anonymous class through the new class, which can be used to replace some complete class definitions that are "burned after use.

echo (new class() {    public function myMethod() {      return "Hello!";    }})->myMethod();//Result: Hello!echo (new class() {    public function myMethod() {      return "Hello!";    }})->myMethod();//Result: Hello!

4. more Error errors can be handled.

More errors in PHP 7 are returned to the developer as caught exceptions. If no capture is performed, the Error is returned. If the capture is performed, the Exception can be processed in the program. By default, errors directly cause program interruptions, while PHP 7 captures and processes the program through the try/catch block, so that the program can continue to be executed, providing programmers with more flexible options.

Sample code:

nonExistFunction($arg); // It will generate fatal errornonExistFunction($arg); // It will generate fatal error

The above code will prompt the error "Fatal error: Call to a member function method () on a non-object", and the Fatal error will stop subsequent code execution.

If you want to continue executing the code, you can solve the problem through exception handling:

try {    nonExistFunction($arg); //This method is not exists then it will be go to catch} catch (EngineException $e ) {    echo "Exception: {$e->getMessage()}n";}try {    nonExistFunction($arg); //This method is not exists then it will be go to catch} catch (EngineException $e ) {    echo "Exception: {$e->getMessage()}n";}

5. combined with comparison operators <=>

Let's look at the sample code. you can easily understand the role of this operator through the code.

// Before PHP 7: compare two numbers of function func ($ a, $ B) {return ($ a <$ B )? -1: ($ a> $ B )? 1: 0)} // New PHP operator <=> function func ($ a, $ B) {return $ a <=> $ B ;} // Before PHP 7: compare two numbers of function func ($ a, $ B) {return ($ a <$ B )? -1: ($ a> $ B )? 1: 0)} // New PHP operator <=> function func ($ a, $ B) {return $ a <=> $ B ;}

6. define an array constant

In the past, when we used define () to define constants, data types only support scalar, but in PHP7, constants of the array type can be defined.

define('MYCONSTANT', array('a','b','c'));define('MYCONSTANT', array('a','b','c'));

There are many new features in PHP7. we will introduce them here today, and we will continue to update them later. We also welcome the majority of PHPer to add. we will share, learn, and make progress together.

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.