Double not (!!) operator in PHP

后端 未结 6 1260
广开言路
广开言路 2020-11-29 17:47

What does the double not operator do in PHP?

For example:

return !! $row;

What would the code above do?

6条回答
  •  無奈伤痛
    2020-11-29 18:41

    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
    

提交回复
热议问题