PHP 中的双不 (!!) 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2127260/
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
Double not (!!) operator in PHP
提问by Andrei
What does the double notoperator do in PHP?
PHP中的double not运算符有什么作用?
For example:
例如:
return !! $row;
What would the code above do?
上面的代码会做什么?
回答by Theo
It's not the "double not operator", it's the notoperator applied twice. The right !will result in a boolean, regardless of the operand. Then the left !will negate that boolean.
这不是“双重非运算符”,而是应用了两次的非运算符。无论操作数是什么,右边!都会产生一个布尔值。然后左边将否定那个布尔值。!
This means that for any true value (numbers other than zero, non-empty strings and arrays, etc.) you will get the boolean value TRUE, and for any false value (0, 0.0, NULL, empty strings or empty arrays) you will get the boolean value FALSE.
这意味着对于任何真值(零以外的数字、非空字符串和数组等),您将获得布尔值TRUE,而对于任何假值(0、0.0 NULL、空字符串或空数组),您将获得布尔值FALSE。
It is functionally equivalent to a cast to boolean:
它在功能上等同于强制转换为boolean:
return (bool)$row;
回答by viraptor
It's the same (or almost the same - there might be some corner case) as casting to bool. If $rowwould cast to true, then !! $rowis also true.
它与强制转换为 bool 相同(或几乎相同 - 可能存在一些极端情况)。如果$row将强制转换为 true,则!! $row也是 true。
But if you want to achieve (bool) $row, you should probably use just that - and not some "interesting" expressions ;)
但是如果你想实现(bool) $row,你可能应该只使用那个 - 而不是一些“有趣”的表达;)
回答by YOU
It means if $rowhas a truthyvalue, it will return true, otherwise false, converting to a boolean value.
这意味着如果$row有一个真值,它将返回true,否则false,转换为布尔值。
Here is example expressions to boolean conversion from php docs.
这是从 php 文档到布尔值转换的示例表达式。
Expression Boolean
$x = ""; FALSE
$x = null; FALSE
var $x; FALSE
$x is undefined FALSE
$x = array(); FALSE
$x = array('a', 'b'); TRUE
$x = false; FALSE
$x = true; TRUE
$x = 1; TRUE
$x = 42; TRUE
$x = 0; FALSE
$x = -1; TRUE
$x = "1"; TRUE
$x = "0"; FALSE
$x = "-1"; TRUE
$x = "php"; TRUE
$x = "true"; TRUE
$x = "false"; TRUE
回答by Steve Bennett
"not not" is a convenient way in many languages for understanding what truth value the language assigns to the result of any expression. For example, in Python:
在许多语言中,“not not”是一种方便的方式,用于理解语言赋予任何表达式结果的真值。例如,在Python 中:
>>> not not []
False
>>> not not [False]
True
It can be convenient in places where you want to reduce a complex value down to something like "is there a value at all?".
在您想要将复杂值减少到诸如“是否有值?”之类的地方,它会很方便。
回答by Luca Reghellin
Another more human, maybe simpler, way to 'read' the not not:
另一种更人性化的,也许更简单的,“阅读” not not 的方法:
The first '!' does 2 things: 'convert' the value to boolean, then output its opposite. So it will give true if the value is a 'falsy' one.
The second '!' is just to output the opposite of the first.
首先 '!' 做两件事:将值“转换”为布尔值,然后输出相反的值。因此,如果该值为“假”值,它将为真。
第二 '!' 只是输出与第一个相反的结果。
So, basically, the input value can be anything, maybe a string, but you want a boolean output, so use the first '!'. At this point, if you want TRUE when the input value is 'falsy', then stop here and just use a single '!'; otherwise if you want TRUE when the input value is 'truthy', then add another '!'.
所以,基本上,输入值可以是任何东西,可能是一个字符串,但你想要一个布尔输出,所以使用第一个“!”。此时,如果您希望在输入值为 'falsy' 时为 TRUE,那么就到此为止,只需使用一个 '!'; 否则,如果您希望在输入值为 'truthy' 时为 TRUE,则添加另一个 '!'。
回答by Chris Rutledge
Lets look at
让我们看看
!$a;
Rather than interpreting the ! operator as as taking the
而不是解释!运营商作为采取
Boolean opposite of the valueto its right
与其右边的值相反的布尔值
read
读
take the Boolean opposite of the expressionto its right
取其右边的表达式的布尔相反值
In this case
在这种情况下
$a;
could be an expression
可能是一种表达
so to is
所以是
!$a;
so is
也是
!!$a;
and
和
!!!$a;
and so on, as each of these is a valid expression, the ! operator can be prepended to each of them.
等等,因为这些都是有效的表达式,! 运算符可以添加到它们中的每一个。

