Double not (!!) operator in PHP

后端 未结 6 1283
广开言路
广开言路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 18:45

    Lets look at

    !$a;
    

    Rather than interpreting the ! operator as as taking the

    Boolean opposite of the value to its right

    read

    take the Boolean opposite of the expression to 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.

提交回复
热议问题