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.

7
  • 1
    an array would be a start here. Commented Mar 27, 2017 at 13:09
  • No you cannot do this way. Commented Mar 27, 2017 at 13:10
  • I know that way not work, this is just example how i think to short it. Commented Mar 27, 2017 at 13:11
  • @PhpDev can you write some exmp.? Commented Mar 27, 2017 at 13:11
  • There is no native function for this. You could try the approaches here, stackoverflow.com/questions/4993104/… or write your own. Commented Mar 27, 2017 at 13:13

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.