Check for an empty variable

Discussion in 'PHP' started by Handsofmodder, Sep 21, 2008.

  1. #1
    How do I check for an empty variable in php using an if statement?
     
    Handsofmodder, Sep 21, 2008 IP
  2. BMR777

    BMR777 Well-Known Member

    Messages:
    145
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    140
    #2
    Try:

    if($variable == "" or $variable == NULL){
    // Do Something
    }
    PHP:
    BMR777
     
    BMR777, Sep 21, 2008 IP
  3. Handsofmodder

    Handsofmodder Peon

    Messages:
    177
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Okay. Lately I tried to make a code to check to see if a variable has a cookie inside it then display, Welcome "cookie name". But, it won't execute properly! Here's the code

    <?php
    $me=$_COOKIE['user'];
    if(strlen($_COOKIE['user'])==0){
    setcookie('user','david',60,'/');
    print "Cookie Not There!";
    }else{
    print "Welcome  ".$me;
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    </body>
    </html>
    
    PHP:
    You see the code in action here
     
    Handsofmodder, Sep 22, 2008 IP
  4. zeldaze

    zeldaze Active Member

    Messages:
    311
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Try
    Uses the isset function.
     
    zeldaze, Sep 22, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    I normally user isset() for cookies
     
    ads2help, Sep 22, 2008 IP
  6. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #6
    AsHinE, Sep 22, 2008 IP
  7. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Use the empty function:

    www.php.net/empty

    Edit: Appears someone just beat me to it. Here's an example to make up for it:

    if(empty($_COOKIE['user'])){
    setcookie('user','david',60,'/');
    print "Cookie Not There!";
    }else{
    print "Welcome  ".$me;
    }
    PHP:
    Jay
     
    jayshah, Sep 22, 2008 IP
  8. minghuhuang

    minghuhuang Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i can use empty()
     
    minghuhuang, Sep 23, 2008 IP
  9. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #9
    to be honest, you can even do if (!$_COOKIE['name']) {... } which will be fine - will evaluate as false if it's not set. only danger is if it IS set and the value is 0 or false.

    isset() is the safest way to go.
     
    dimitar christoff, Sep 23, 2008 IP