简体   繁体   English

什么是 null 合并分配??= PHP 7.4 中的运算符

[英]What is null coalescing assignment ??= operator in PHP 7.4

I've just seen a video about upcoming PHP 7.4 features and saw this new ??= operator.我刚刚观看了有关即将推出的 PHP 7.4 功能的视频,并看到了这个新的??=运算符。 I already know the ??我已经知道了?? operator.操作员。

How's this different?这有什么不同?

In PHP 7 this was originally released, allowing a developer to simplify an isset() check combined with a ternary operator.PHP 7 中,这是最初发布的,允许开发人员结合三元运算符来简化 isset() 检查。 For example, before PHP 7, we might have this code:例如,在 PHP 7 之前,我们可能有这样的代码:

$data['username'] = (isset($data['username']) ? $data['username'] : 'guest');

When PHP 7 was released, we got the ability to instead write this as:PHP 7发布时,我们可以将其写为:

$data['username'] = $data['username'] ?? 'guest';

Now, however, when PHP 7.4 gets released, this can be simplified even further into:然而,现在,当PHP 7.4发布时,这可以进一步简化为:

$data['username'] ??= 'guest';

One case where this doesn't work is if you're looking to assign a value to a different variable, so you'd be unable to use this new option.这不起作用的一种情况是,如果您想为不同的变量分配一个值,那么您将无法使用这个新选项。 As such, while this is welcomed there might be a few limited use cases.因此,虽然这很受欢迎,但可能有一些有限的用例。

From the docs :文档

Coalesce equal or ??=operator is an assignment operator. Coalesce equal 或 ??=operator 是赋值运算符。 If the left parameter is null, assigns the value of the right paramater to the left one.如果左参数为空,则将右参数的值分配给左参数。 If the value is not null, nothing is done.如果该值不为空,则什么都不做。

Example:例子:

// The folloving lines are doing the same
$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';
// Instead of repeating variables with long names, the equal coalesce operator is used
$this->request->data['comments']['user_id'] ??= 'value';

So it's basically just a shorthand to assign a value if it hasn't been assigned before.因此,如果之前未分配过值,则它基本上只是分配值的简写。

The null coalescing assignment operator is a shorthand way of assigning the result of the null coalescing operator.空合并赋值运算符是分配空合并运算符结果的一种简写方式。

An example from the official release notes :官方发行说明中的一个示例:

$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault();
}

Null coalescing assignment operator chaining:空合并赋值运算符链接:

$a = null;
$b = null;
$c = 'c';

$a ??= $b ??= $c;

print $b; // c
print $a; // c

Example at 3v4l.org 3v4l.org 上的示例

Example Docs :示例文档

$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault();
}

?? is a conditional operator, and it will always result in one of two values. 是条件运算符,它将始终导致两个值之一。

$a = 'previous value';
$a = arr['test'] ?? 'previous value';

??= on the other hand is an assignment operator ??=另一方面是赋值运算符

$a = 'previous value';
$a ??= arr['test']

If arr['test'] is null nothing happens, $a keeps 'previous value' . 如果arr['test']null什么都不会发生, $a保留'previous value' If arr['test'] is not null , $a will get the value out of the array. 如果arr['test']不为null ,则$a将值从数组中取出。

Working fiddle . 工作提琴

You can use this to initialize variables during a loop's first iteration.您可以使用它在循环的第一次迭代期间初始化变量。 But beware!但要小心!

$reverse_values = array();
$array = ['a','b','c']; // with [NULL, 'b', 'c'], $first_value === 'b'
foreach($array as $key => $value) {
  $first_value ??= $value; // won't be overwritten on next iteration (unless 1st value is NULL!)
  $counter ??= 0; // initialize counter
  $counter++;
  array_unshift($reverse_values,$value);
}
// $first_value === 'a', or 'b' if first value is NULL
// $counter === 3
// $reverse_values = array('c','b','a'), or array('c','b',NULL) if first value is null

If the first value is NULL , then $first_value will be initialized to NULL and then overwritten by the next non- NULL value.如果第一个值为NULL ,则$first_value将被初始化为NULL ,然后被下一个非NULL值覆盖。 If the array has a lot of NULL values, $first_value will end up either as NULL or the first non- NULL after the last NULL .如果数组有很多NULL值, $first_value将最终为NULL或最后一个NULL之后的第一个非NULL So this seems like a terrible idea.所以这似乎是一个可怕的想法。

I would still prefer doing something like this mainly because it's more clear, but also because it works with NULL as an array value:我仍然更喜欢做这样的事情,主要是因为它更清晰,但也因为它可以将NULL作为数组值使用:

$reverse_values = array();
$array = ['a','b','c']; // with [NULL, 'b', 'c'], $first_value === NULL
$counter = 0;
foreach($array as $key => $value) {
  $counter++;
  if($counter === 1) $first_value = $value; // does work with NULL first value
  array_unshift($reverse_values,$value);
}

In PHP 7.4 you can do: 在PHP 7.4中,您可以执行以下操作:

$array['value'] ??= 'someValue';

instead of

$array['value'] = $array['value'] ?? 'someValue';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM