What the different between if(!isset()) and if(isset())

Discussion in 'PHP' started by ketting00, Jan 24, 2010.

  1. #1
    Hi. I'm confusing. Can somebody help me, please.
    Sometimes this snippet works: if(!isset($_SESSION['login'])) {
    Sometimes this works: if(isset($_SESSION['login'])) {

    Which one choose I use?

    Thanks
     
    ketting00, Jan 24, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    if(!isset($_SESSION['login'])) { = determines if the $_SESSION is NOT set.

    if(isset($_SESSION['login'])) { = determines if the $_SESSION IS set.

    Example:

    
    <?php
    if(isset($_SESSION['login'])) {
    
    echo "You are logged in!";
    } else {
    
    echo "You are not logged in!";
    }
    ?>
    
    PHP:
    ...

    
    <?php
    if(!isset($_SESSION['login'])) {
    
    echo "You are not logged in!";
    } else {
    
    echo "You are logged in!";
    }
    ?>
    
    PHP:
    So you could say its in reverse order - but both can be used to achieve the same thing.
     
    danx10, Jan 24, 2010 IP
  3. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ! mean is not
     
    astkboy2008, Jan 24, 2010 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    The choice of which to use is based on what you want the next lines after the conditional to do. Look closely at dan's example above.
     
    Colbyt, Jan 24, 2010 IP
  5. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #5
    Thanks, that gives an idea which one I should go with.
     
    ketting00, Jan 24, 2010 IP
  6. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    actually, if (!isset($_SESSION['login'])) technically doesn't really mean "if SESSION['login'] is NOT set"...in fact it's more correct to translate it as, "if the expression isset($_SESSION['login']) returns false". you can drive yourself crazy with comparisons if you misinterpret the actual meaning of an expression.
     
    szalinski, Apr 17, 2010 IP
  7. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i always use empty() instead of isset() :) :D
     
    trickyshaun, Apr 18, 2010 IP
  8. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #8
    !isset is looking for FALSE to be returned from the function, whereas the other way will check for TRUE.
     
    lukeg32, Apr 18, 2010 IP
  9. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #9
    isset will return TRUE if the checked variable is not NULL. Meaning that isset(false) would still return true.
    empty however returns TRUE if the checked variable is NULL, FALSE, '' (empty string) or an empty array. empty is also implicitly used in if statement.
     
    Gray Fox, Apr 19, 2010 IP
  10. job0107

    job0107 Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    isset() means the variable exists whether it has a value in it or not.
    !isset() means the variable doesn't exist yet.
    Most times, better to use empty().
     
    job0107, Apr 19, 2010 IP
  11. X-N2O

    X-N2O Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    ! is the NOT operator, it inverses the value of a boolean expression
    if isset were to return true, !isset would not be true
    if isset were to be false, !isset would be true
     
    X-N2O, Apr 19, 2010 IP