tags:

views:

303

answers:

4

Hi guys, does anybody know what the double not operator does in PHP

ex: return !! $row;

What would the code above do?

Thanks

+8  A: 

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 is also true.

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

viraptor
Thank's that actualy makes sense, I nevers seen something like this in other programing language.
andreeib
Using `!!` is a habit remaining from programming in C(++). In C doing a cast isn't as easy as in PHP, you can get many different problems, compile warnings, a.s.o. Thus people cast to boll by using `!!`.
nikic
@nikic - It sounds strange, IMO... since `operator!` is overloadable, using it in `C++` would be dangerous. Also you cannot cast structs in C, or use `!` with them. Also if something can be negated, you're most likely able to return it from a function returning int, which pretty much makes it a boolean. Are you sure you're talking about C?
viraptor
@viraptor: Hum, I'm no C programmer, admittedly. But I have seen some commits in software there a cast was replaced by `!!` and assumed that that's due to compiler warnings. Now I researched a little bit and found this: [http://stackoverflow.com/questions/206564/what-is-the-performance-implication-of-converting-to-bool-in-c](Performance Warnings in VS).
nikic
+3  A: 

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

S.Mark
+2  A: 

Sounds like it's an abbreviation of (which in term could be made a ternary operator)

if ($value) { // if 'truthy'
    return true;
} else {
    return false;
}

It's a more semanticly correct value to be returned in some circumstances.

It would be much easier to use (bool). I wouldn't recommend using the !! prefix, as it may confuse a future developer for a while. They'll probably come across this page via Google.

alex
`!!` is used in a few languages for casting to a boolean. I don't know if I'd recommend against using it. Most devs love using shorthand code.
Andy E
@Andy E Thanks for letting me know. I'm mainly just a PHP/JavaScript guy so that explains my ignorance. I love shorthand too, but when `(bool)` does the same and is recognised a lot more often, I'd tend to recommend it.
alex
+3  A: 

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 right ! 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.

Theo
"double-not-operator" or "double not-operator" .. depends how you read it.
nickf
Thank's alot, I understand now your explication was the best.
andreeib
@nickf not really, "the double not operator" refers to one operator, not the act of using the not operator twice.
Theo