How to check if a variable has a value?

Discussion in 'PHP' started by archard, Aug 31, 2007.

  1. #1
    I need PHP code that would check a variable and see whether or not it's blank. I'm using Drupal and CCK and I want the code to check and see if there is a value set for that field.
     
    archard, Aug 31, 2007 IP
  2. WebGeek182

    WebGeek182 Active Member

    Messages:
    510
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    95
    #2
    You can use isset or check to see if its value is null or "". Check out php.net for usage. Also, while testing, you can have it print to html (echo) to manually check if everything is working.
     
    WebGeek182, Aug 31, 2007 IP
  3. archard

    archard Peon

    Messages:
    221
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah I've already checked out isset but it's not able to tell whether or not a value has been entered for the variable. It can only tell if the variable exists.
     
    archard, Aug 31, 2007 IP
  4. ssanders82

    ssanders82 Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ssanders82, Aug 31, 2007 IP
  5. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The problem with functions like empty() and isset() is that they make assumptions (sometimes terrible ones) about the data value. empty(), for example, returns TRUE for integers with value '0', even though '0' is, technically, a "value".

    And isset() returns FALSE for null, whereas null might also sometimes be considered a valid value.

    The bottom line is that it really depends on context.

    For every variable you need validate, be it string or integer or float or array or whatever, you might need to do several different kinds of checks and tests, all based on context.

    Such are the vagaries of a loosely-typed language.
     
    sea otter, Aug 31, 2007 IP