If statement......

Discussion in 'PHP' started by karl_murphy, May 2, 2008.

  1. #1
    Hi,

    What is the best way to write an if statement to check whether a variable is empty? The variable in question is a field of a table retrieved from a database.

    Any ideas?
     
    karl_murphy, May 2, 2008 IP
  2. NuLLByTe

    NuLLByTe Active Member

    Messages:
    382
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    80
    #2
    
    if ($variable = "")
    {
    ...
    };
    
    PHP:
    If I am wrong, correct me.
     
    NuLLByTe, May 2, 2008 IP
  3. ggggqqqqihc

    ggggqqqqihc Peon

    Messages:
    92
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Why not use empty()?
    
    if (empty($var)){
      // ....
    }
    
    PHP:
     
    ggggqqqqihc, May 2, 2008 IP
  4. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #4
    theres a better way, php has a built in function for doing this

    If (empty($some_var)){
    	//code here will execute if $some_var is empty
    }
    PHP:
    if you wanted to see if it wasn’t empty (i.e. it has a value) then do

    If (!empty($some_var)){
    	//code here will execute if $some_var has something in it
    }
    PHP:
    mor einfo here: http://uk3.php.net/manual/en/function.empty.php
     
    lanmonkey, May 2, 2008 IP
  5. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    CreativeClans, May 2, 2008 IP
  6. NuLLByTe

    NuLLByTe Active Member

    Messages:
    382
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Thanks for your tips lanmonkey.
     
    NuLLByTe, May 2, 2008 IP
  7. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Basically I know certain values in the database are set to NULL. How would I check if a variable was not NULL? Is this the same as empty?
     
    karl_murphy, May 2, 2008 IP
  8. me4you

    me4you Well-Known Member

    Messages:
    1,989
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    155
    #8
    me4you, May 2, 2008 IP
    getjimmy likes this.
  9. Bronto

    Bronto Peon

    Messages:
    308
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It should be:
    
    if ($variable == "")
    PHP:
     
    Bronto, May 2, 2008 IP
  10. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #10
    null is NOT the same as empty. you would use the inbuilt php function is_null, so in your case you would use:

    if (!is_null($var)){
    //code here will execute if $var is NOT null
    }

    http://uk3.php.net/manual/en/function.is-null.php
     
    lanmonkey, May 5, 2008 IP