PHP 中的 C# 空合并运算符 (??)
PHP中有没有类似C#的??
的三元运算符之类的?
C# 中的 ??
干净且较短,但在 PHP 中您必须执行以下操作:
// This is absolutely okay except that $_REQUEST['test'] is kind of redundant.
echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi';
// This is perfect! Shorter and cleaner, but only in this situation.
echo null? : 'replacement if empty';
// This line gives error when $_REQUEST['test'] is NOT set.
echo $_REQUEST['test']?: 'hi';
Is there a ternary operator or the like in PHP that acts like ??
of C#?
??
in C# is clean and shorter, but in PHP you have to do something like:
// This is absolutely okay except that $_REQUEST['test'] is kind of redundant.
echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi';
// This is perfect! Shorter and cleaner, but only in this situation.
echo null? : 'replacement if empty';
// This line gives error when $_REQUEST['test'] is NOT set.
echo $_REQUEST['test']?: 'hi';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
PHP 7 添加了 空合并运算符:
您还可以查看编写 PHP 三元运算符 ?: (仅限 PHP >=5.3)
并且您与 C# 的比较是不公平的。 “在 PHP 中你必须做类似的事情” - 在 C# 中,如果你尝试访问不存在的数组/字典项,你也会遇到运行时错误。
PHP 7 adds the null coalescing operator:
You could also look at short way of writing PHP's ternary operator ?: (PHP >=5.3 only)
And your comparison to C# is not fair. "in PHP you have to do something like" - In C# you will also have a runtime error if you try to access a non-existent array/dictionary item.
空合并运算符,(
??
) 已在 PHP 7。它不同于 短三元运算符 (? :
) 中的??
将抑制E_NOTICE
,否则在尝试访问没有键的数组时会发生这种情况。 RFC 中的第一个示例给出:请注意,
??
运算符不需要手动应用isset
来防止E_NOTICE
。The Null Coalesce Operator, (
??
) has been accepted and implemented in PHP 7. It differs from the short ternary operator (?:
) in that??
will suppress theE_NOTICE
that would otherwise occur when attempting to access an array where it doesn't have a key. The first example in the RFC gives:Notice that the
??
operator does not require the manual application ofisset
to prevent theE_NOTICE
.我用的是函数。显然它不是运算符,但看起来比你的方法更干净:
用法:
I use function. Obviously it is not operator, but seems cleaner than your approach:
Usage:
在 PHP 7 之前,没有。如果您需要涉及
isset
,则使用的模式是isset($var) ? $var:空
。没有包含isset
特征的?:
运算符。Prior to PHP 7, there isn't. If you need to involve
isset
, the pattern to use isisset($var) ? $var : null
. There's no?:
operator that includes the characteristics ofisset
.??
在 C# 中是二进制的,而不是三进制的。在 PHP 7 之前的 PHP 中,它没有等效项。??
is binary in C#, not ternary. And it has no equivalence in PHP prior to PHP 7.从 PHP 5.6 开始,不存在相同的运算符,但您可以创建一个行为类似的函数。
用法:
这将尝试每个变量,直到找到满足
isset()
的变量:$option_override
$_REQUEST['option']
$_SESSION['option']
false
如果 4 不存在,则默认为
null
。注意:有一个使用引用的更简单的实现,但它具有 将测试项设置为 null(如果尚未设置为 null)的副作用存在。当数组的大小或真实性很重要时,可能会出现问题。
An identical operator doesn't exist as of PHP 5.6, but you can make a function that behaves similarly.
Usage:
This would try each variable until it finds one that satisfies
isset()
:$option_override
$_REQUEST['option']
$_SESSION['option']
false
If 4 weren't there, it would default to
null
.Note: There's a simpler implementation that uses references, but it has the side effect of setting the tested item to null if it doesn't already exist. This can be problematic when the size or truthiness of an array matters.