A detailed explanation of the null merge operator in PHP, phpnull operator _php Tutorial

Source: Internet
Author: User

A detailed explanation of the null merge operator in PHP, phpnull operator


The null merge operator is a good thing, and with it we can easily get a parameter and provide a default value in case it is empty. For example, in JS can be used in | | To engage in:

function Setsomething (a) {  a = a | | ' Some-default-value ';  // ...}

And in PHP, unfortunately PHP's | | Always returns TRUE or FALSE, unable to do so.

PHP7 only formally joined the party?? This operator:

Get the value of the user parameter (if empty, use ' nobody ') $username = $_get[' user ']?? ' nobody ';//equivalent to: $username = isset ($_get[' user ')? $_get[' user ']: ' nobody ';

PHP7 estimate there is still a long time to use the production environment, there is no alternative in the current PHP5?

According to the study, there is a very convenient alternative:

Get the value of the user parameter (if empty, use ' nobody ') $username = @$_get[' user ']?: ' nobody ';//equivalent to: $username = isset ($_get[' user ')? $_get[' user ']: ' nobody ';

--Run this code: HTTPS://3V4L.ORG/ADUW8

Staring at the eyes, with the PHP7 of the previous example of the similar, mainly to the?? Replace for?:. What kind of a ghost is this? Actually this is (EXPR1)? (EXPR2): ellipsis mode for (EXPR3) expression:

An expression (EXPR1)? (EXPR2): (EXPR3) when EXPR1 evaluates to TRUE, the value is EXPR2, and the value is EXPR1 when the EXPR3 evaluates to FALSE.

From PHP 5.3, the middle part of the ternary operator can be omitted. Expression expr1?: Expr3 returns EXPR1 when EXPR1 evaluates to TRUE, otherwise returns EXPR3.

--http://php.net/manual/zh/language.operators.comparison.php

Of course, this alternative is not perfect-if there is no ' user ' in $_get, there will be a notice:undefined index:user error, so you need to use @ To suppress the error, or to turn off E_notice's error.

PS:PHP7 null merge operator Farewell Isset ()

The previous wording

$info = isset ($_get[' email ')? $_get[' email ': ' Noemail ';

Just write this right now.

$info = $_get[' email '?? Noemail;

You can also use this ligatures

$info = $_get[' email '?? $_post[' email '?? ' Noemail ';

Articles you may be interested in:

    • C # null merge operator?? (double question mark) using the example

http://www.bkjia.com/PHPjc/1086639.html www.bkjia.com true http://www.bkjia.com/PHPjc/1086639.html techarticle A detailed explanation of the null merge operator in PHP, the phpnull operator null merge operator is a good thing, with it we can easily get a parameter, and can be in the case of empty ...

  • Related Article

    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.