Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help understanding && and ||

    This script is working but I do not understand it. I am thinking that the || comparison should be used, but the only way it will give the right result is with &&.

    This is what I am seeing: If neither $_SESSION['id'] or $_SESSION['edit'] are set I want the statement to return TRUE. Therefore I think I should have to use ||. I am thinking that by using the && comparison that both would have to not be set for the statement to return true.

    PHP Code:
    <?php

    $_SESSION
    ['id'] = '12345';
    // $_SESSION['edit'] = 'update';

    if (!isset($_SESSION['id']) && !isset($_SESSION['edit']))
    {
        echo 
    "<br/>Neither session is set.";
        exit();
    }
    else echo 
    "Good to go!";
    ?>
    Please explain.

    Sam
    ;
    The person who coin the phrase, "There is no such thing as a stupid question.", has never meet me.

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    You contridict your self:
    Code:
    neither $_SESSION['id'] or $_SESSION['edit']
    Neither implies that you want to check that both $_SESSION['id'] AND $_SESSION['edit'] are not set.

    If you use OR (||) the if statement will return true if only one of the session variables is not set.

    If you use AND(&&) both other the session variables will have to be not set in order to return true.
    Last edited by srule_; 01-04-2008 at 04:16 PM.

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I took another look at it and played with it for half an hour. It is working the way it should. What I want to do is have it return TRUE if neither are set. So by checking both with AND if one is set then it will return false.

    I appreciate you reading and responding.

    Problem Solved!
    Thanks,
    Samantha
    The person who coin the phrase, "There is no such thing as a stupid question.", has never meet me.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    There's actually a name for this, it's called De Morgan's Law. It states that the condition !(A or B) is equal to the condition (!A and !B). So if you want to test to see if A is false or B is false, you have to use &&.

    To get it straightened out in my brain, when I had a brain that is, I replaced each expression in the condition with a "T" or an "F", then looked at the "if" statement again, using the "T"'s and "F"s.

    PHP Code:
    //assuming:
    $_SESSION['id'] = "something";
    unset(
    $_SESSION['edit']);

    if (!isset(
    $_SESSION['id']) || !isset($_SESSION['edit'])) 
    //replacing expressions with T or F:
    //if (F || T)   //this evaluates to T

    if (!isset($_SESSION['id']) && !isset($_SESSION['edit'])) 
    //replacing expressions with T or F:
    //if (F && T)   //this evaluates to F 
    Edit: Whoops, looks like you have it figured out. I got distracted in the middle of my reply


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •