In PHP7 we should learn new features that will be used

Source: Internet
Author: User
Tags scalar

PHP7 is officially released every month, this update is an important milestone in PHP, It will bring significant performance improvements and new features, and some features of the previous version of the improvements. This article will be with you to learn about The new features in PHP7.

1. scalar type declaration

< Span style= "font-size:14px" > We know Php is a weakly typed programming language , So there is no way to specify the type of input parameter and return value, PHP7 int,float,string,bool declare (strict_types=1) directive declaration is strict type checking, let's look at a code:

1 Declare function $x $y) : int {2     return$x$y; 3 echo Add (1, 2);  // Int (7)

valid types are:class/interface name, self, array, callable, bool, float, int, and string.

2.?? --NULL Merge Operators

PHP7 added the NULL merge operator, do not underestimate this "?? "With it we can easily get a parameter and provide a default value in case it is empty." How ?? the left value of the operator exists and is not NULL, the left side is returned, otherwise the right value is returned. Let's try the following code . operator is powerful.

<?php
//




?>

3. Anonymous class

The anonymous class, as the name implies, has no class names, and its declaration and instantiation are simultaneous, PHP7 supports instantiating an anonymous class with the new class, which can be used instead of some " Burn after " class definitions.

Echo (newclass() {    publicfunction  myMethod () {        return "hello!" ;    }}) // result:hello!

4. more error errors can be handled with exception

more error in PHP7 becomes a Exception that can be captured and returned to the developer, Error if no capture is made , and if the capture becomes a process that can be processed within the program Exception. By default,Error causes the program to break directly, while the PHP7 is captured and processed by the Try/catch block, allowing the program to continue executing and providing a more flexible choice for programmers.

code example:

Nonexistfunction ($arg//  It generate fatal error

At this point the code above will indicate the error "Fatal Error:call to a member function method () on a Non-object", and this fatal error will stop the subsequent execution of the code.

So if you want to continue executing the code, you can resolve it by exception handling:

Try  {    nonexistfunction ($arg//This method isn't exists then it'll be' go to catch} catch (Engineexception $e) {    echo "Exception: {$e->getmessage ()}\n";}

5. Combining comparison operators (<=>)

This is not a lot to explain, we look directly at the sample code, you can easily understand the role of the operator through the code.

// before PHP 7: Compare the size of the two number function func ($ A, $ b) {   return ($a$b)? -1: (($a$b)? 1:0//  PHP new operator <=> function func ($ A, $ b) {
   return$a$b;}

6. defining array Constants

In the past, when we defined constants with define (), the data type only supported scalars, but in PHP7 , constants that defined the array type were supported.

Define Array (' A ', ' B ', ' C '));

PHP7 new features There are many, today we introduced to this, follow-up will continue to update, but also welcome the vast number of phper Supplement, we share, common learning, common progress.

In PHP7 we should learn new features that will be used

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.