Home  >  Article  >  Backend Development  >  Detailed explanation of the null coalescing operator in PHP, detailed explanation of the phpnull operator_PHP tutorial

Detailed explanation of the null coalescing operator in PHP, detailed explanation of the phpnull operator_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:081029browse

Detailed explanation of the null merging operator in PHP, detailed explanation of the phpnull operator

The null merging operator is a good thing. With it, we can easily obtain a parameter. And can provide a default value if it is empty. For example, you can use || in js:

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

In PHP, unfortunately, PHP's || always returns true or false, so it cannot be done this way.

PHP7 has just officially added this operator:

// 获取user参数的值(如果为空,则用'nobody')
$username = $_GET['user'] ?? 'nobody';
// 等价于:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

It is estimated that PHP7 will take a long time to be used in production environments. So are there any alternatives to the current PHP5?

According to research, there is a very convenient alternative:

// 获取user参数的值(如果为空,则用'nobody')
$username = @$_GET['user'] ?: 'nobody';
// 等价于:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

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

Looking at it with wide eyes, it is similar to the previous PHP7 example, mainly replacing ?? with ?: . What the hell is this? In fact, this is the omission pattern of (expr1) ? (expr2) : (expr3) expression:

Expression (expr1) ? (expr2) : (expr3) has the value expr2 when expr1 evaluates to TRUE and expr3 when expr1 evaluates to FALSE.

Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE and expr3 otherwise.

-- 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 this error, or turn off the E_NOTICE error.

ps: PHP7 null coalescing operator Say goodbye to isset()

Previous writing method

$info = isset($_GET['email']) ? $_GET['email'] : ‘noemail';

Just write like this now

$info = $_GET['email'] ?? noemail;

You can also write it in conjunction like this

$info = $_GET['email'] ?? $_POST['email'] ?? ‘noemail';

Articles you may be interested in:

  • C# null coalescing operator?? (double question mark) usage example

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1086639.htmlTechArticleDetailed explanation of the null coalescing operator in PHP, detailed explanation of the phpnull operator The null coalescing operator is a good thing, with With it, we can easily get a parameter, and when it is empty...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn