Double not (!!) operator in PHP

What does the double not operator do in PHP?

For example:

return !! $row;

What would the code above do?


It's not the "double not operator", it's the not operator 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 .

It is functionally equivalent to a cast to boolean :

return (bool)$row;

It's the same (or almost the same - there might be some corner case) as casting to bool. If $row would cast to true, then !! $row !! $row is also true.

But if you want to achieve (bool) $row , you should probably use just that - and not some "interesting" expressions ;)


It means if $row has a truthy value, it will return true , otherwise false , converting to a boolean value.

Here is example expressions to boolean conversion from php docs.

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
链接地址: http://www.djcxy.com/p/1726.html

上一篇: PHP中@符号的用法是什么?

下一篇: 在PHP中双重(!!)运算符