isset or not, NULL or not.

Discussion in 'PHP' started by asmon, Feb 25, 2008.

  1. #1
    i am wondering when should i use 'isset', for example
    if(isset($_COOKIE['ID']) ...
    if($_COOKIE['ID']) ...

    will give me the same result so why and when should i put isset?


    same thing about having a NULL or not.
    both returns false in a 'if' statement and both are equal to ""
    but the difference is when i put 'isset', then the empty cell returns
    true while the NULL returns false. how does it help me?!
    and again, where should i leave an empty cell and where do i put a NULL

    my last question is about die();
    i put it in every query - "query... or die();"
    i understand why but are there other cases other than queries that can fail and i should put die?
    ty.
     
    asmon, Feb 25, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    if ($_COOKIE['x'])

    Does work, but it leaves an E_NOTICE if the variable is not defined. That means each time you do this, and the variable is not defined, PHP will open the error log, and write to it, and close it, and throw an error. Therefore it's a bad practice to use this method. isset() will avoid that if the variable is not set. You can see these E_NOTICES if you turn on full error reporting. You can do that by placing this line at the top of your code:
    
    error_reporting(E_ALL);
    
    PHP:
    isset() returns false if a variable has the value NULL.
    
    $foo = null;
    
    if (isset($foo))
    {
        echo 'Is set';
    }
    else
    {
        echo 'Is not set';
    }
    
    PHP:
    The above would output "ist not set", even though the variable is defined. If you want to check for null values, use is_null() instead.

    Read this for more info: www.php.net/isset

    EDIT:

    When programming, you should always put the error reporting to E_ALL, and make sure no errors are shown, under any circumstances. That's the best practice.
     
    nico_swd, Feb 25, 2008 IP
  3. Im The ONE

    Im The ONE Peon

    Messages:
    800
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There's a difference isset means a variable is defined or not while ($_cookie['x']) will check its null or not
     
    Im The ONE, Feb 25, 2008 IP
  4. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the tip.
     
    Morishani, Feb 25, 2008 IP
  5. asmon

    asmon Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    let me see if i got this isset thing straight.
    i had a code which looked like this

    $level = $_GET["level"];
    
    if ($level != "")
    {
    .......
    }
    else {......}
    PHP:
    now i had to change it so i won't get any errors and it looks like this

    if (isset($_GET["level"])) {$level = $_GET["level"];}
    
     if (isset($level))
     {....}
    else {....}
    PHP:
     
    asmon, Feb 25, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    I'd do it this way:
    
    if (!empty($_GET['level'])) // empty() does not throw errors either: www.php.net/empty
    {
        ....
    }
    
    PHP:
    There's no need to assign the $_GET['xxx'] variables to a local variable. I would only do that if you need the variable multiple times, and you're too lazy to type out $_GET...

    A second way would be:
    
    if (!isset($_GET['level']))
    {
        $_GET['level'] = null; // Give it a default value.
    }
    
    if ($_GET['level'])
    {
       ...
    }
    
    PHP:
    And a third way:
    
    $level = isset($_GET['level']) ? $_GET['level'] : null;
    
    if ($level)
    {
        ...
    }
    
    PHP:

    If the variable has been defined with the variable null, you can skip isset(), and use the method above. It won't trigger errors.
     
    nico_swd, Feb 25, 2008 IP
  7. asmon

    asmon Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    "I would only do that if you need the variable multiple times"
    that's exactly the reason, and i like when it looks simple without all this $_GET...
    (just noticed i didn't have to check isset twice but nvm)

    thanks for the detailed explanation.
     
    asmon, Feb 25, 2008 IP
  8. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #8
    4rd shortest way I personally prefer:

    $level = @$_GET['level'];
    if ($level)
    {
       ...
    }
    PHP:
     
    wmtips, Feb 25, 2008 IP