Null union operator in PHP-php Tutorial

Source: Internet
Author: User
PHP's null merge operator project: blogtarget: null-coalesce-operator-in-php.mddate: 2015-12-30status: publishtags:-NullCoalesce-PHPcategories:-PHPnull merge operator is a good thing, with this, we can easily use the null merge operator in PHP.
project: blogtarget: null-coalesce-operator-in-php.mddate: 2015-12-30status: publishtags:    - Null Coalesce    - PHPcategories:    - PHP

The null merge operator is a good thing. with it, we can easily obtain a parameter and provide a default value when it is null. For example, you can use||Here:

function setSomething(a){    a = a || 'some-default-value';    // ...}

In PHP, it's a pity that||Always returntrueOrfalse.
PHP7 was officially added.??This operator:

// Obtain the value of the user parameter (if it is null, use 'nobody') $ username = $ _ GET ['user']? 'Nobody'; // equivalent to: $ username = isset ($ _ GET ['user'])? $ _ GET ['user']: 'Nobody ';

PHP7 is estimated to be used in the production environment for a long time. Is there any alternative solution in the current PHP5?
According to research, there is a very convenient alternative:

// Obtain the value of the user parameter (if it is null, use 'nobody') $ username = @ $ _ GET ['user']? : 'Nobody'; // equivalent to: $ username = isset ($ _ GET ['user'])? $ _ GET ['user']: 'Nobody ';

-- Run this code: https://3v4l.org/aDUW8

With a Wide Eye, the above PHP7 example is similar, mainly??To?:. What is this? In fact, this is(expr1) ? (expr2) : (expr3)Expression omitting mode:

Expression (expr1 )? (Expr2): (expr3) when the value of expr1 is TRUE, the value is expr2, and when the value of expr1 is FALSE, the value is expr3.
From PHP 5.3, the portion in the middle of the ternary operator can be omitted. Expression expr1? : Expr3 returns expr1 when the value of expr1 is TRUE; otherwise, expr3 is returned.
Http://php.net/manual/zh/language.operators.comparison.php

Of course, this alternative is not perfect-if$_GETNot in'user', There will be oneNotice: Undefined index: userSo you need to use@To suppress or disable this error.E_NOTICE.

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.