Keverw
Keverw

Reputation: 3786

Why would this php not work but would work this way?

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

Works But

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

Wont work. Why is this? I want the code to fire off if both Ids don't match.

Upvotes: 4

Views: 86

Answers (4)

amccormack
amccormack

Reputation: 13927

You are using the unary operator ! which only operates on the value $sess_uid. So you are negating the value of $sess_uid. What you are probably looking for is to use != instead.

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

Upvotes: 1

Vilx-
Vilx-

Reputation: 106912

This:

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

Is equivalent to this:

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

While you want:

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

Upvotes: 4

codaddict
codaddict

Reputation: 454970

! is having higher precedence than ==.

!a==b is treated as (!a) == b

What you need is !(a==b) which is same as a != b

Upvotes: 5

MatTheCat
MatTheCat

Reputation: 18721

if (!$sess_uid == $WallID) don't equals if ($sess_uid !== $WallID)

Upvotes: 0

Related Questions