Meaning of the operator?

Asked

Viewed 107 times

4

What is the function of by one !! in an if for example? know what ! by itself reverses the value of a boolean result, but tested the !! and nothing changed in the result, example:

$teste = true;
if(!!$teste) {
   echo "é verdade!";
} else {
   echo "é mentira!";
}

and resulted in true that if

1 answer

3


The operator ! denies. If I put another ! before, I’ll be denying the first denial.

That is to say:

If i == verdade and !i == falsa, then !!i == verdade

I could have more denial operators before.

I mean, I could have that:

$teste = true;
if(!!!!$teste) {
    echo "é verdade!";
} else {
    echo "é mentira!";
}

... that works well. I would be denying a denial.

  • 1

    That doesn’t explain what the double negative is for. Here you can see more details: http://answall.com/questions/29014/qual-sentido-de-usar-dupla-nega%C3%A7%C3%a3o-em-javascript

  • So it’s no use if you take the !! is the same thing as not putting. Right ?

  • 1

    @Zoom vide link above. It is a kind of "cast".

  • !! is what is called dupla negação, or if you prefer double not operator. In practice... what would I use it for? No.. I know. Maybe it’s for educational purposes

  • 1

    It’s just another php nonsense. You can do it too var_dump(!!!!!!!!!!!!!!!!!!!!!! false != true)

  • 2

    @Wallacemaxters is not a wacky, it’s a cast way to bool value.

  • 1

    @Since I would never use that expression, it’s crazy. If you want to cast, php already has a mechanism for this: the function boolval php 5.6, or the cast operator (bool). It is not necessary to use double negation in PHP. This is a thing of languages like javascript.

  • 3

    @Wallacemaxters you would not use, others would use. Question of readability and simplicity.

  • For me it’s silly too. Neither in PHP, nor in Javascript would use this paranauê. Who agrees gives a Like.

  • 3

    @Wallacemaxters and Zoom have nothing to do with the language. It is used in JS, is used in PHP, and is a technique known in several other languages. Note that the OP example is not a case for use, it is usually used within more complex expressions to force the predictability of the result without leaving the code all over the place. If you don’t want to use it, just don’t use it. Others do. Just like adding up zero in string, to always get numeric. Explicit and implicit cast are normal things.

  • @Bacco, Defender of Logical Operators of PHP. Joke... But the good thing about some languages is this, several ways to perform various functions. But then, when I develop something, I try my best to avoid conversions.

  • 2

    @Zoom is not defending no, you can use it badly too (the question has an example of bad use). It is that the way you spoke, a reader who does not know the utility can think that it is useless, just so I commented. So at least some curious will have to research further, and not go out repeating what they read (neither of you nor of me) kkk

  • @Wallacemaxters, other example => http://answall.com/q/4018/91

  • @Wallacemaxters want a simple example of what can be useful : $arrayPopulado = !!array();

  • @Guillhermelautert $arrayPopulado = (bool) array()

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.