Best test for empty form value ?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-19-2004
Cyrus D.
 
Posts: n/a
Default Best test for empty form value ?

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


Reply With Quote
  #2 (permalink)  
Old 10-20-2004
john.postlethwait@gmail.com
 
Posts: n/a
Default Re: Best test for empty form value ?

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.

Reply With Quote
  #3 (permalink)  
Old 10-20-2004
 
Posts: n/a
Default Re: Best test for empty form value ?

> 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
Reply With Quote
  #4 (permalink)  
Old 10-20-2004
 
Posts: n/a
Default Re: Best test for empty form value ?


> $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
Reply With Quote
  #5 (permalink)  
Old 10-20-2004
Tony Marston
 
Posts: n/a
Default Re: Best test for empty form value ?

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
>
>



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:08 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0