Best pracrtice for evaluating a null variable

Discussion in 'PHP' started by Tintumol, Dec 23, 2010.

  1. #1
    Hello,

    I have a variable that could be a string or it could be null:

    Then I want to evaluate $pageid:

    Is that ok to do, considering $pageid might be null? I heard of putting an @ sign in front to ignore warnings if $pageid is null. Is that the best way to do it? Seems sloppy to me.
     
    Last edited: Dec 23, 2010
    Tintumol, Dec 23, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    I do something similar, but I use the ternary:

    $pageid = isset($_GET['pageid']) ? intval($_GET['pageid']) : NULL;
    PHP:
    Its not sloppy, its better then getting an undefined error.

    You shouldn't need to put @ to supress errors, as it should'nt give you any errors as the variable is SET.
     
    danx10, Dec 23, 2010 IP
  3. getbestproduct

    getbestproduct Peon

    Messages:
    140
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if(isset($_GET['pageid']))
    {
    $pageid = $_GET['pageid'];
    }
    else
    {
    $pageid ='';
    }


    if($pageid !== 'mypage')
    {
    die();
    }
     
    getbestproduct, Dec 23, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    if(isset() && !is_null())
     
    tvoodoo, Dec 23, 2010 IP