Assuming that your data is coming from $_GET
or $_POST
, all data fields will be strings. This means that you should be able to do the check in a single function call:
if (in_array('', $list, TRUE)) {
$error[] = "All fields are required.";
}
This looks for strings which are exactly equal to an empty string. If you want to make the comparisons loose (more or less identical to the check that empty()
does) just remove the final TRUE
.
EDIT Thinking about it, you don't need the strict comparison. I did this to allow for a legitimate field value of '0'
(which empty()
would not allow) but this will also be permitted with loose comparisons, since '0' != ''
.
ANOTHER EDIT If you want to check that the length of the sting is greater than two, you will have to loop:
foreach ($list as $item) {
if (strlen($item) < 2) {
$error[] = "All fields are required.";
break;
}
}
This will also "clear out 0
" assuming that by this you mean "not allow a value to be 0
". If you also want to disallow '00'
(or any other string that results in 0
) you can change the if
clause to this:
if (strlen($item) < 2 || (!(int) $item)) {