php PHP中的双问号(??)运算符是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53610622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:54:29  来源:igfitidea点击:

What does double question mark (??) operator mean in PHP

phpoperator-keyword

提问by elkolotfi

I was diving into Symfony framework (version 4) code and found this peace of code:

我正在深入研究 Symfony 框架(第 4 版)代码,并发现代码的这种平静:

$env = $_SERVER['APP_ENV'] ?? 'dev';

I'm not pretty sure what this actually does but I imagine that it expands to something like:

我不太确定这实际上做了什么,但我想它会扩展到以下内容:

$env = $_SERVER['APP_ENV'] != null ? $_SERVER['APP_ENV'] : 'dev';

Or maybe:

或者可能:

$env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';

Someone has any precision about the subject?

有人对这个主题有任何精确吗?

EDIT:

编辑:

To all the people who answered the question: thank you To all the people who marked my question as negative because there's already a similar question (PHP ternary operator vs null coalescing operator):

感谢所有回答问题的人:谢谢所有将我的问题标记为否定的人,因为已经有一个类似的问题(PHP 三元运算符 vs 空合并运算符):

It is true that both questions are very similar. However it is hard for everybody to imagine that the "??" is called the coalescing operator.

这两个问题确实非常相似。然而,每个人都很难想象“??” 称为合并运算符。

Otherwise I could easy find it on the official documentation:

否则我可以很容易地在官方文档中找到它:

http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

However, for someone who didn't know that this feature was added in php 7 it's more likely to type:

但是,对于不知道在 php 7 中添加此功能的人来说,更有可能键入:

"php ?? operator" or "php double question mark operator"

“php ?? 运算符”或“php 双问号运算符”

And here is why my question has an added value.

这就是为什么我的问题具有附加值的原因。

I ask you to, please, reconsider your negative feedback. Thanks

我请你重新考虑你的负面反馈。谢谢

Regards, Epixilog

问候, Epixilog

回答by michalhosna

It's the "null coalescing operator", added in php 7.0. The definition of how it works is:

它是 php 7.0 中添加的“空合并运算符”。它如何工作的定义是:

It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

如果存在且不为 NULL,则返回其第一个操作数;否则它返回它的第二个操作数。

So it's actually just isset()in a handy operator.

所以它实际上只是isset()一个方便的操作符。

Those two are equivalent1:

这两个等价于1

$foo = $bar ?? 'something';
$foo = isset($bar) ? $bar : 'something';

Documentation: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce

文档:http: //php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce

In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

在 PHP7 新特性列表中:http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

And original RFC https://wiki.php.net/rfc/isset_ternary

和原始 RFC https://wiki.php.net/rfc/isset_ternary



EDIT: As this answer gets a lot of views, little clarification:

编辑:由于这个答案得到了很多观点,所以很少澄清:

1There is a difference: In case of ??, the first expression is evaluated only once, as opposed to ? :, where the expression is first evaluated in the condition section, then the second time in the "answer" section.

1有一个区别:在 的情况下??,第一个表达式只计算一次,而不是? :,其中表达式首先在条件部分计算,然后在“答案”部分计算第二次。

回答by Cid

$myVar = $someVar ?? 42;

Is equivalent to :

相当于:

$myVar = isset($someVar) ? $someVar : 42;


For constants, the behaviour is the same when using a constant that already exists:

对于常量,使用已经存在的常量时的行为是相同的:

define("FOO", "bar");
define("BAR", null);

$MyVar = FOO ?? "42";
$MyVar2 = BAR ?? "42";

echo $MyVar . PHP_EOL;  // bar
echo $MyVar2 . PHP_EOL; // 42

However, for constants that don't exist, this is different :

但是,对于不存在的常量,这是不同的:

$MyVar3 = IDONTEXIST ?? "42"; // Raises a warning
echo $MyVar3 . PHP_EOL;       // IDONTEXIST

Warning: Use of undefined constant IDONTEXIST - assumed 'IDONTEXIST' (this will throw an Error in a future version of PHP)

警告:使用未定义的常量 IDONTEXIST - 假定为“IDONTEXIST”(这将在 PHP 的未来版本中引发错误)

Php will convert the non-existing constant to a string.

Php 会将不存在的常量转换为字符串。

You can use constant("ConstantName")that returns the value of the constant or null if the constant doesn't exist, but it will still raise a warning. You can prepended the function with the error control operator@to ignore the warning message :

constant("ConstantName")如果常量不存在,您可以使用返回常量或 null 的值,但它仍然会引发警告。您可以在函数前面加上错误控制运算符@以忽略警告消息:

$myVar = @constant("IDONTEXIST") ?? "42"; // No warning displayed anymore
echo $myVar . PHP_EOL; // 42

回答by Prodigle

$x = $y ?? 'dev'

is short hand for x = y if y is set, otherwise x = 'dev'

如果设置了 y,则是 x = y 的简写,否则 x = 'dev'

There is also

还有

$x = $y =="SOMETHING" ? 10 : 20

meaning if y equals 'SOMETHING' then x = 10, otherwise x = 20

意思是如果 y 等于 'SOMETHING' 那么 x = 10,否则 x = 20