Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6837399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:28:21+00:00 2026-05-26T23:28:21+00:00

I have the following code: $_SESSION[‘user_role’] = 1; if ($_SESSION[‘user_role’] != ‘1’ || $_SESSION[‘user_role’]

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:28:22+00:00Added an answer on May 26, 2026 at 11:28 pm

    Because user_role cannot be 1 and 2 at the same time. Let’s go through all possible values:

    1: != 2
    2: != 1
    anything else: != 1, != 2
    

    You probably wanted to write your condition as follows

    if(!($_SESSION['user_role'] == 1 || $_SESSION['user_role'] == 2)) {
      return false;
    } else {
      return true;
    }
    

    which is equivalent to:

    if($_SESSION['user_role'] != 1 && $_SESSION['user_role'] != 2)) {
      return false;
    } else {
      return true;
    }
    

    (boolean algebra)

    Your condition can even be written without an if:

    return $_SESSION['user_role'] == 1 || $_SESSION['user_role'] == 2;
    

    PS. don’t use magic values


    For the sake of completeness, here’s a truth table:

    A := user_role == 1
    B := user_role == 2
    
      | A | B | !B | !A | A or B | !A or !B | !(A or B) | !A and !B
    --+---+---+----+----+--------+----------+-----------+-----------
    1 | T | F |  T |  F |      T |        T |        F  |         F
    2 | F | T |  F |  T |      T |        T |        F  |         F
    --+---+---+----+----+--------+----------+-----------+-----------
    3 | F | F |  T |  T |      F |        T |        T  |         T
    4 | F | F |  T |  T |      F |        T |        T  |         T
    5 | F | F |  T |  T |      F |        T |        T  |         T
    ...
    

    From the table you can immediately see that !(A or B) is equivalent to !A and !B

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a header.html which start with session_start();. Then the following code, $_SESSION['cart'][$sw_id] is
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following php code: <?php session_start(); .... $result=$db->query($query); $row=$result->fetch_assoc(); $_SESSION['id']=$row['id']; header('Location: http://www.blabla.com/successLoginPage.php');
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];
I have the following code: $this->db->from('addresses'); $this->db->where('user_id', $this->session->userdata('user.id')); $this->db->like('country', $pSearch); $this->db->or_like('state', $pSearch); $this->db->or_like('city', $pSearch);
I have the following code which works fine. However, I only want to return
I have following PHP code: if($_SESSION['msg']['login-err']) { echo '<p class=error>'.$_SESSION['msg']['login-err'].'</p>'; unset($_SESSION['msg']['login-err']); } Everything working
I have the following code: public static string FormatHostAndUrl(this HttpSessionStateBase session) { var a
Currently I have the following code: switch (publicationType) { case PublicationType.Book: return Session.Query<Publication>() .Where(p
I have the following code: var redirectIp = string.Format({0}{1}, Session[CurrentHost], ip.PathAndQuery); return new RedirectResult(redirectIp);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.