Why does this php not work, but will work like that?

if ($sess_uid == $WallID)
{
//
}
else
{
Run Action
}

Work but

if (!$sess_uid == $WallID)
{
Run Action
}

Does not work. Why is this? I want the code to fire if both identifiers do not match.

+3
source share
4 answers

It:

if (!$sess_uid == $WallID)
{
    Run Action
}

This is equivalent to this:

if ( (!$sess_uid) == ($WallID))
{
    Run Action
}

While you want:

if (!($sess_uid == $WallID))
{
    Run Action
}
+4
source

!has a higher precedence than ==.

!a==b regarded as (!a) == b

You need !(a==b)which is equala != b

+5
source

You are using a unary operator! which only affects the value of $ sess_uid. So you are denying the value of $ sess_uid. What you are probably looking for is to use! = Instead.

if (!$sess_uid != $WallID){ Run Action }

+1
source
if (!$sess_uid == $WallID) don't equals if ($sess_uid !== $WallID)
0
source

Source: https://habr.com/ru/post/1768405/


All Articles