Alberto Favaro
Alberto Favaro

Reputation: 1840

Better method to write a long logical condition in PHP

How could I improve the following statement:

if(!empty($var1) && !empty($var2) && !empty($var3) && !empty($var4) && !empty($var5) && !empty($var6) && !empty($var7)....)

Is there a PHP function that allows chaining of logical conditions?

Thanks for your suggestions.

Upvotes: 2

Views: 169

Answers (6)

Jared Dunham
Jared Dunham

Reputation: 1527

I would create an array of variables to check and loop to see if any of them are empty.

$check = array(
    'var1',
    'var2',
    'var3',
    'var4',
    'var5',
    'var6',
    'var7',
);


$empty = false;
foreach($check as $value){
    if(empty(${$value})){
        $empty = true;
        break;
    }
}

if($empty){
    // there is an empty value
}

Upvotes: 1

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38542

You can try this way also,

$not_empty = false;
$required_fields = [$var1,$var2,$var3,$var4,$var5,$var6,$var7];
foreach ($required_fields as $f) {
    if (!empty($f)) {
       $not_empty = true;
    }else{
       $not_empty = false;
       break;
    }
}

if($not_empty){
  //good to go
}

Upvotes: 4

grimgrom
grimgrom

Reputation: 780

Maybe this solution: https://stackoverflow.com/a/13329035/1824143

function has_empty(array $array)
{
    return count($array) != count(array_diff($array, array('', null, array())));
}

$vars = [$var1,$var2,$var3,$var4,$var5,$var6,$var7];
if(!has_empty($vars))
{
  ...
}

Upvotes: 1

thordarson
thordarson

Reputation: 6261

The answers submitted so far could be written up into a little utility function. Here's an untested function that takes a variable amount of arguments.

function all_empty() {
  // Grab the number of arguments passed to the function.
  $numberOfArguments = func_num_args();
  $arguments = func_get_args();
  $output = true;

  // Return if nothing was passed.
  if($numberOfArguments == 0) {
    return;
  }

  // Check each passed argument, break on failure.
  foreach($arguments as $argument) {
    if(!empty($argument)) {
      $output = false;
      break;
    }
  }

  return $output;
}

Using similar methods, we can write a function called none_empty:

function none_empty() {
  // Grab the number of arguments passed to the function.
  $numberOfArguments = func_num_args();
  $arguments = func_get_args();
  $output = true;

  // Return if nothing was passed.
  if($numberOfArguments == 0) {
    return;
  }

  // Check each passed argument, break on failure.
  foreach($arguments as $argument) {
    if(empty($argument)) { // This is the only difference.
      $output = false;
      break;
    }
  }

  return $output;
}

Usage examples:

if(all_empty($var1, $var2, $var3)) { /* They're all empty */ }
if(!all_empty($var1, $var2, $var3)) { /* Some of them aren't empty */ }
if(none_empty($var1, $var2, $var3)) { /* They all have a value */ }
if(!none_empty($var1, $var2, $var3)) { /* Some of them are empty */ }

And since we wrote the functions to be able to take a variable amount of arguments, this is possible as well:

if(all_empty($var1, $var2, $var3, $var4, $var5, ...)) { }

Upvotes: 2

JrmC
JrmC

Reputation: 360

I guess you could do something like that :

$n = 7;
$empty = false;
foreach(range(1, $n) as $v)
{
    if(empty(${'var' . $v})){
        $empty = true;
    }
}

if(!$empty){
    echo 'no empty variables';
} 

Upvotes: 2

Ad5001
Ad5001

Reputation: 753

To be more readable, you could make "groups" (could be one or multiple) of conditions and divide it to make multiple lines:

if(!empty($var1) &&
   !empty($var2) &&
   !empty($var3) &&
   !empty($var4) &&
   !empty($var5) &&
   !empty($var6) &&
   !empty($var7) ...) {

What you could also do with issets and unsets, you could shorten:

if(!isset($var1) &&
   !isset($var2) &&
   !isset($var3) &&
   !isset($var4) &&
   !isset($var5) &&
   !isset($var6) &&
   !isset($var7) ...) {

to

if(isset($var1, $var2, $var3, $var4, $var5, $var6, $var7)) {

Upvotes: 1

Related Questions