A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearBetter way to check variable for null or empty string? - page 1
User InfoPosts
Better way to check variable for null or empty string?#1
Since PHP is a dynamic language what's the best way of checking to see if a provided field is empty?

I want to ensure that:


null is considered an empty string
a white space only string is considered empty
that "0" is not considered empty


This is what I've got so far:

$question = trim($_POST['question']);

if ("" === "$question") {
// Handle error here
}


There must be a simpler way of doing this?

posted date: 2008-12-19 07:24:00


Re: Better way to check variable for null or empty string?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-19 07:24:01


Re: Better way to check variable for null or empty string?#3
// Function for basic field validation (present and neither empty nor only white spacefunction IsNullOrEmptyString($question){ return (!isset($question) || trim($question)==='');}

posted date: 2008-12-19 07:27:00


Re: Better way to check variable for null or empty string?#4
Although this solves it, I'm not sure if it's simpler. +1 anyway

posted date: 2008-12-19 07:36:00


Re: Better way to check variable for null or empty string?#5
Since OP is asking for a 'simpler' version of an extremely simple operation, I'm going to say that 'better' is what's actually warranted.

posted date: 2008-12-19 07:49:00


Re: Better way to check variable for null or empty string?#6
I turned it into a function. this should make your validation code simpler

posted date: 2008-12-19 07:51:00


Re: Better way to check variable for null or empty string?#7
There is no better way but since it's an operation you usually do quite often, you'd better automatize the process.Most frameworks offer a way to make arguments parsing an easy task. You can build you own object for that. Quick and dirty example :class Request{ // This is the spirit but you may want to make that cleaner :-) function get($key, $default=null, $from=null) { if ($from) : if (isset(${'_'.$from}[$key])); return sanitize(${'_'.strtoupper($from)}[$key]); // didn't test that but it should work else if isset($_REQUEST[$key]) return sanitize($_REQUEST[$key]); return $default; } // basics. Enforce it with filters according to your needs function sanitize($data) { return addslashes(trim($data)); } // your rules here function isEmptyString($data) { return (trim($data) === "" or $data === null); } function exists($key) {} function setFlash($name, $value) {} [...]}$request = new Request();$question= $request->get('question', '', 'post');print $request->isEmptyString($question);Symfony use that kind of sugar massively. But you are talking about more than that, with your "// Handle error here". You are mixing 2 jobs : getting the data and processing it. This is not the same at all.There are other mechanisms you can use to validate data. Again, frameworks can show you best pratices.Create objects that represent the data of your form, then attach processses and fall back to it. It sounds far more work that hacking a quick PHP script (and it is the first time), but it's reusable, flexible, and much less error prone since form validation with usual PHP tends to quickly become spaguetti code.

posted date: 2008-12-19 07:58:00


Re: Better way to check variable for null or empty string?#8
I would say to use empty($question), but that also considers 0 to be empty.

posted date: 2008-12-19 08:06:00


Re: Better way to check variable for null or empty string?#9
Beware false negatives from the trim() function — it performs a cast-to-string before trimming, and thus will return e.g. "Array" if you pass it an empty array. That may not be an issue, depending on how you process your data, but with the code you supply, a field named question[] could be supplied in the POST data and appear to be a non-empty string. Instead, I would suggest:$question = $_POST['question'];if (!is_string || ($question = trim($question))) { // Handle error here}// If $question was a string, it will have been trimmed by this point

posted date: 2008-12-19 09:58:00


Re: Better way to check variable for null or empty string?#10
Id say if you get an array where you expected a string it should be an error. If you expect an array then you should have a separate filtering function for that.

posted date: 2008-12-19 16:41:00


Re: Better way to check variable for null or empty string?#11
You did the exact opposite of what he wanted... simplicity.

posted date: 2008-12-19 19:46:00


Re: Better way to check variable for null or empty string?#12
Old post but someone might need it as I did ;)if (strlen($str) == 0){do what ever}replace $str with your variable.NULL and "" both return 0 when using strlen.

posted date: 2010-01-20 08:59:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.