[thelist] PH isset and is_empty

Steve Lewis nepolon at worlddomination.net
Thu Jul 3 14:04:03 CDT 2003


fstorr wrote:

>I'm trying to do some basic PHP form validatation.  I'm passing a
>variable through the the form processing page and picking it up fine
>  
>

>the user has entered nothing (I know that I can just do if (!$var) but I
>want to explore further).
>
>Looking at the manual, it seems the following should work:
>If (isset($var))
>But if I type in nothing, it still returns true, which I don't
>  
>
--isset($var) will tell you if the name "var" is a defined variable.

--(!$var) will tell you if the value of the variable "var" has a "false" 
(false is defined as any of { 0, null, the empty string aka "" ) value.  
Technically, you should do both,

if (!isset($var) || (!$var))
  // than the user has entered nothing

to determine if the user has entered something because you cannot depend 
on the client to send an empty value on a form submit.  Checkboxes are 
only sent if they are checked, and the spec says that regardless of 
type, inputs need not send anything if they are not set.

<tip>
you could embed this logic into a handy function!

function fieldnotset($var)
{
    return (!isset($var) || (!$var));
}

[...]

if fieldnotset($var) {
    // user left the field blank
}
</tip>




More information about the thelist mailing list