双问号(??)运算符在PHP中是什么意思 [英] What does double question mark (??) operator mean in PHP

查看:605
本文介绍了双问号(??)运算符在PHP中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我不太确定这实际上是做什么的,但我想它会扩展为:

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

或者:

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

有人对这个主题有什么精确度吗?

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

两个问题的确很相似.但是,每个人都很难想象"??"被称为合并运算符.

否则,我可以在官方文档中轻松找到它:

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

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

"php ??运算符"或"php双问号运算符"

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

请您重新考虑您的负面反馈. 谢谢

关于, Epixilog

解决方案

它是php 7.0中添加的空合并运算符".其工作方式的定义是:

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

所以实际上它只是方便使用的运算符中的isset().

这两个是等效的 1 :

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

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

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

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


编辑:由于该答案获得了很多见解,因此很少澄清:

1 有一个区别:在??的情况下,第一个表达式仅计算一次,而在? :情况下,第一个表达式仅在条件部分进行计算,然后第二次在答案"部分.

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

$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):

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

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

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

And here is why my question has an added value.

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

Regards, Epixilog

解决方案

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

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

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

Those two are equivalent1:

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

Documentation: 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

And original 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.

这篇关于双问号(??)运算符在PHP中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆