473,383 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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
Jul 17 '05 #1
4 2272
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.

Jul 17 '05 #2
> 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
Jul 17 '05 #3
$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
Jul 17 '05 #4
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." <sa***@invalid.org> wrote in message
news:p7*********************@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

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: n_o_s_p_a__m | last post by:
My xml doc has many <title></title> and <title> in it, meaning the nodes have no content (although some do). How can I test for this? I tried title (doesn't work) I tried //title (doesn't work)...
6
by: WindAndWaves | last post by:
Hi Folks I have inhereted a script that I understand reasonably well, I just do not understand !/^\d+$/.test(el.value) what the hell does that mean? Below is the script (there are really...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
7
by: Gertjan van Heijst | last post by:
Hi, I really hope someone can help me because I've already spend 2 days on this problem and I'm not getting anywhere. I think the problem is that I don't really understand how text boxes store...
6
by: EDOnLine | last post by:
New asp.net/webform user question...... I am trying to use the Custom Validator control to check to see if either of two fields on my form have been filled in. At this point all I am interested...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
1
by: Muchach | last post by:
Hello, Ok so what I've got going on is a form that is populated by pulling info from database then using php do{} to create elements in form. I have a text box in each table row for the user to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.