I have the following code:
$_SESSION['user_role'] = 1;
if ($_SESSION['user_role'] != '1' || $_SESSION['user_role'] != '2') {
return false;
} else {
return true;
}
Why does this if() always returns false?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because
user_rolecannot be 1 and 2 at the same time. Let’s go through all possible values:You probably wanted to write your condition as follows
which is equivalent to:
(boolean algebra)
Your condition can even be written without an if:
PS. don’t use magic values
For the sake of completeness, here’s a truth table:
From the table you can immediately see that
!(A or B)is equivalent to!A and !B