Jacob
Jacob

Reputation: 2071

if or statement isn't working correctly

Im having problem with a if statement. This is the if statement:

if($userID != $_SESSION["sess_id"] || $creator != $_SESSION["sess_id"]){

echo "Success";
exit;

}

This is the values for the variables:

$userID = 24;
$creator = 13;
$_SESSION["sess_id"] = 13;

So for short:

if(24 != 13 || 13 != 13){

echo "Success";
exit;

}

Why is Success still showing?

Note that this is just a shortened version of my code, No one of the variables are set like this but the values are right.

Upvotes: 0

Views: 64

Answers (4)

Krish R
Krish R

Reputation: 22711

The expression $userID != $_SESSION["sess_id"] -> 24 != 13 is satisfied(TRUE) so that it pass into the condition, You have used OR condition, either any one condition true block get executed.

Upvotes: 0

user3037249
user3037249

Reputation: 66

because you use the "||". It's an "or". You must use "&&" (and).

if($userID != $_SESSION["sess_id"] && $creator != $_SESSION["sess_id"]){

Upvotes: 1

Johan Lindkvist
Johan Lindkvist

Reputation: 1784

Because you are using ||OR and that only needs one statement to be true and in your case 24 != 13 is true

Upvotes: 1

kero
kero

Reputation: 10638

Because 24 != 13 is still true. For OR it suffices that one of the sides is true.

if (true || false) //true
if (true && false) //false
if (false || false) //false
if (false && false) //false

Upvotes: 5

Related Questions