Are all the fields in $_POST empty?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pilaftank
Forum Newbie
Posts: 4
Joined: Thu Feb 23, 2006 6:40 pm

Are all the fields in $_POST empty?

Post by pilaftank »

Is there an easy way in PHP to determine if all the values of a string array are empty? More specifically, how can you detect if there is no form data in the $_POST array.

You could do something like the following.

Code: Select all

$Nothing = true;
   foreach ($_POST as $Field=>$Value)
      $Nothing = $Nothing && strlen($Value) == 0;
But that seems awfully cumbersome for such a simple test. There must be an easier way.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

You could work something out with http://us2.php.net/manual/en/function.array-filter.php

Or just put your code into a function..

You could also call array_unique which will remove all the duplicates (leaving you with an array of one empty element)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

an explicit list is the best (most accurate) way.

Code: Select all

<?php

if ((!isset($_POST['var'])) || (empty($_POST['var']))) {
  // do something in response to $_POST['var'] not being complete
}

?>
Rinse/repeat for all your expected variables.
pilaftank
Forum Newbie
Posts: 4
Joined: Thu Feb 23, 2006 6:40 pm

Post by pilaftank »

jshpro2 wrote:You could work something out with http://us2.php.net/manual/en/function.array-filter.php

Or just put your code into a function..

You could also call array_unique which will remove all the duplicates (leaving you with an array of one empty element)
Well, the array_filter() idea is interesting, but that requires writing a separate function. So, its complexity is definitely greater than that of the foreach solution (which already seems too complex for such a simple task).

However, the array_unique approach looks a lot more interesting. The code array_unique($_POST) would presumably return an array with only one element whose value would be the empty string. But how would I actually test that in a boolean statement?

By the way, the reason that I'm looking for the simplest, cleanest solution for this problem is that the code will be part of an FAQ accessed by a lot of people (and I need to keep tech support to a minimum!).
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

pilaftank wrote:But how would I actually test that in a boolean statement?
empty()
pilaftank
Forum Newbie
Posts: 4
Joined: Thu Feb 23, 2006 6:40 pm

Post by pilaftank »

Jenk wrote:an explicit list is the best (most accurate) way.
Not in this case. An explicit check is brittle and prone to errors (and more importantly for me, it requires lots of painful explaining in the FAQ about how to use the code).
pilaftank
Forum Newbie
Posts: 4
Joined: Thu Feb 23, 2006 6:40 pm

Post by pilaftank »

jshpro2 wrote:
pilaftank wrote:But how would I actually test that in a boolean statement?
empty()
Yeah, but the array is not actually empty. Instead, the string in the array is empty (and we don't now the name of the array element that holds the string). I'm sure the code is very simple, I'm just not sure exactly what it looks like (and I confess I'm really a Java developer not a PHP developer).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

using array_filter() does not require you to code a function.

Code: Select all

$filtered = array_filter($array, 'strlen');
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

reset($array);
var_dump(empty(current($array)));
Feyd, I like your approach
pilaftank wrote:
jshpro2 wrote:
pilaftank wrote:But how would I actually test that in a boolean statement?
empty()
Yeah, but the array is not actually empty. Instead, the string in the array is empty (and we don't now the name of the array element that holds the string). I'm sure the code is very simple, I'm just not sure exactly what it looks like (and I confess I'm really a Java developer not a PHP developer).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
foreach($_POST as $post_key => $post_val) {
    if ( empty($post_val) ) {
        echo "POST VAR key $post_key is empty!<br />";
    }
}
?>
Will this do what you want?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Everah, that has more overhead then just looking at the current item,

we are testing for an array of 1 item that is empty, so if you count() the array and get anything other then 1, we can immediatly assume it has other non-empty values. If we get one then we reset() the array and look at the value of current(), this avoids the overhead of operating on a copy of the array for foreach() and it also avoids the check altogether if it is a multi-item array.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If the count() of the array is anything other than 0 then there are array keys in the array. Whether those keys are attached to values is unknown without looking at the values of each key. For example...

Code: Select all

<?php
$test_array = array(
	"key1" => '',
	"key2" => '',
	"key3" => '',
	"key4" => '',
	"key5" => '');
?>
... yields an array count of 5 eventhough each array item is empty.

As far as the original post...
pilaftank wrote:Is there an easy way in PHP to determine if all the values of a string array are empty? More specifically, how can you detect if there is no form data in the $_POST array.
If the form was submitted by way of a button and the button had any text on it, that button will be sent as a POST var and it will set its value in the POST array. If there were any other fields in the form, they will also set themselves in the POST array even if they are empty. Each item in the POST will be reflected in the count() of the POST array var. To see if the POST array vars are empty, you need to walk the array (at least I thought so).

Please correct me if I am wrong.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

For those that want that "optimum" line of code

Code: Select all

$hasEmpty = (count($_POST) != count(array_filter($_POST,'strlen')));
:P
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Evarah, I think you missed that we were using array_unique on the array before the code, if after array_unique there is more then 1 item then all the $_POST cannot be empty. Anyways I like the array_filter() approach and that is probably the most efficient way as its built in
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

jshpro2 wrote:Evarah, I think you missed that we were using array_unique on the array before the code, if after array_unique there is more then 1 item then all the $_POST cannot be empty. Anyways I like the array_filter() approach and that is probably the most efficient way as its built in
Sorry, I missed the array_unique. I agree with you on using array_filter().
Post Reply