In PHP7 we should learn new features that will be used

Source: Internet
Author: User
Tags getmessage scalar
PHP7 brings significant performance improvements and new features, as well as improvements to some features of previous releases. This article will work with you to explore the new features in PHP7.

1. Scalar type declaration

We know that PHP is a weak type of programming language, so there is no way to specify the type of input parameters and return values, PHP7 breaks This status, adds support for scalar type (int,float,string,bool) declarations, and adds declare (strict_ Types=1) Directive declaration is strict type checking, let's look at a 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 are: Class/interface name, self, array, callable, bool, float, int, and string.

2. Null merge operator

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

<?php//Get the value of the user parameter (if empty, use ' nobody ')//PHP5 We implement this: $username = isset ($_get[' user ')? $_get[' user ']: ' nobody ';//PHP7, use?? Operator more convenient: $username = $_get[' user ']?? ' Nobody ';?? ><?php//Get the value of the user parameter (if empty, use ' nobody ')//PHP5 We implement this: $username = isset ($_get[' user ')? $_get[' user ']: ' nobody ';//PHP7, use?? Operator more convenient: $username = $_get[' user ']?? ' Nobody ';?? >

3. Anonymous class

As the name implies without class names, and its declaration and instantiation are simultaneous, PHP7 supports the instantiation of an anonymous class with the new class, which can be used instead of some "burn after" full class definition.

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 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 exception that can be processed within the program. 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'll generate Fatal Errornonexistfunction ($ARG); It'll 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 is not exists and then it'll be a go to catch} catch (Engineexception $e) {
  echo "Exception: {$e->getmessage ()}n";} try {    nonexistfunction ($arg);//this method is not exists and then it'll be a 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.

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;} 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 (' Myconstant ', Array (' A ', ' B ', ' C '));d efine (' myconstant ', 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.

  • 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.