This is a discussion on Best test for empty form value ? within the PHP Language forums, part of the PHP Programming Forums category; Hi guys, What's the best way to test for an empty form value ? I am doing it like this ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi guys,
What's the best way to test for an empty form value ? I am doing it like this now: $test = $_POST['inputTest']; if(strlen($test) < 1) // it is empty ! Maybe I can just go: if($test == '') // that's two single-quotes with nothing in between // it is empty ! At least I avoid a function call that way. Maybe there is a better way ? Take care, Cyrus |
|
|||
|
Either way should work find, the second way may have less load on the
server, but honestly neither of them would cause too much... So whatever is your preference. I ususally do it the second way and never had a problem with it. |
|
|||
|
> What's the best way to test for an empty form value ? I am doing it like
> this now: > > $test = $_POST['inputTest']; > if(strlen($test) < 1) > // it is empty ! > > Maybe I can just go: > > if($test == '') // that's two single-quotes with nothing in between > // it is empty ! It has been my experience that the function "empty()" has not failed me. If you have a string, "" or '' or "0" is empty. If you have a numeric variable, 0 is empty Arrays with no elements is empty A class property that has been declared but not used (set) is empty A boolean variable that is FALSE is empty NULL values are empty Any object with no properties set is empty http://www.php.net/empty ____________________________________ Wil Moore III, MCP | Integrations Specialist | Assistant Webmaster |
|
|||
|
> $test = $_POST['inputTest']; > if(strlen($test) < 1) > // it is empty ! > > Maybe I can just go: > > if($test == '') // that's two single-quotes with nothing in between > // it is empty ! Also, do not forget, if you are testing for a key in an array such as your example: $_POST['inputTest']; you can do isset($_POST['inputTest']) or you can use array_key_exists('inputTest', $_POST) If you KNOW the key is there, but you just want to know if it has a NULL or "empty" value, your best bet is to run it through the empty() function. ____________________________________ Wil Moore III, MCP | Integrations Specialist | Assistant Webmaster |
|
|||
|
The foolproof way to check for an empty string is always strlen($var) < 1.
You may also want to use trim() to remove any leading/trailing spaces. -- Tony Marston http://www.tonymarston.net "Cyrus D." <satan@invalid.org> wrote in message news:p7gdd.23876$YM4.6134341@news4.srv.hcvlny.cv.n et... > Hi guys, > > What's the best way to test for an empty form value ? I am doing it like > this now: > > $test = $_POST['inputTest']; > if(strlen($test) < 1) > // it is empty ! > > Maybe I can just go: > > if($test == '') // that's two single-quotes with nothing in between > // it is empty ! > > At least I avoid a function call that way. Maybe there is a better way ? > > Take care, > Cyrus > > |