0

I need some help, when i have big form with a lot of data i need check if empty on server side with PHP and i need some short way to make that.

I usually work with IF, but if you have some short way to check is 20 variable empty share with me.

Original request (... dots mean a lot more variable):

if ($a1 != '' && $a2 != '' && $a3 != '' && $a4 != '' && $a5 != '' ...)

Can i write that in some short format ? Like example:

if ($a1,$a2,$a3,$a4,$a5 != '')

Example is just how i thing to short if condition.

1 Answers1

2

The easiest way is to create your own function that checks an array of arguments:

function all_elements_are_empty(...$elements) {
    foreach ($elements as $el) {
        if (!empty($el)) {
            return false;
        }
    }

    return true;
}

The ... operator captures all arguments passed to the function as the $elements array.

To use:

if (all_elements_are_empty($a, $b, $c, ...)) {
    // Do this
} else {
    // Or that
}
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92