Page 1 of 2

Are all the fields in $_POST empty?

Posted: Thu Feb 23, 2006 6:49 pm
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.

Posted: Thu Feb 23, 2006 7:16 pm
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)

Posted: Thu Feb 23, 2006 7:23 pm
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.

Posted: Mon Mar 06, 2006 11:59 pm
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!).

Posted: Tue Mar 07, 2006 12:02 am
by josh
pilaftank wrote:But how would I actually test that in a boolean statement?
empty()

Posted: Tue Mar 07, 2006 12:04 am
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).

Posted: Tue Mar 07, 2006 12:11 am
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).

Posted: Tue Mar 07, 2006 12:22 am
by feyd
using array_filter() does not require you to code a function.

Code: Select all

$filtered = array_filter($array, 'strlen');

Posted: Tue Mar 07, 2006 9:51 pm
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).

Posted: Tue Mar 07, 2006 10:29 pm
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?

Posted: Wed Mar 08, 2006 1:13 pm
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.

Posted: Wed Mar 08, 2006 1:27 pm
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.

Posted: Wed Mar 08, 2006 1:32 pm
by feyd
For those that want that "optimum" line of code

Code: Select all

$hasEmpty = (count($_POST) != count(array_filter($_POST,'strlen')));
:P

Posted: Wed Mar 08, 2006 1:34 pm
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

Posted: Wed Mar 08, 2006 1:45 pm
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().