2

Is there a way to do this, but in a shorter way? or "more professional" way? Thanks and regards. Check variables is empty (check if one is empty).

if((
empty($varone) || 
empty($vartwo) || 
empty($vthree) || 
empty($vardf) || 
empty($vfgsdgsdal) || 
empty($pasadfsd) || 
empty($efda) 
)) {
    $response = "There is an empty data";
    }else{$response = "NO EMPTY DATA!";}
motocross
  • 31
  • 5

2 Answers2

2

Firt solution, add values in array and tests if there is empty value

function checkEmptyValueArray($values) {
    return !empty(array_filter($values, function ($value) {
        return empty($value);
    }));
}

$values = [$varone, $vartwo, $vthree, $vardf, $vfgsdgsdal, $pasadfsd, $efda];

if ( checkEmptyValueArray($values) ) {
    $response = "There is an empty data";
} else{
    $response = "NO EMPTY DATA!";
}

An other solution is to use variable-length arguments list

Follow this link for an explanation : https://www.php.net/manual/en/functions.arguments.php

function checkEmptyValueArray(...$args) {
    foreach ( $args as $arg ) {
        if ( empty($arg) ) {
            return true;
        }
    }
    return false;
}

if ( checkEmptyValueArray($varone, $vartwo, $vthree, $vardf, $vfgsdgsdal, $pasadfsd, $efda) ) {
    $response = "There is an empty data";
} else{
    $response = "NO EMPTY DATA!";
}
Yoleth
  • 1,269
  • 7
  • 15
1

Try this more professional, simple & elegant way:

if (in_array("", array($varone, ..., $varN)) {
   $response = "There is an empty data";
} else {
   $response = "There is not any empty data";
}

Happy coding :)

Rashed Rahat
  • 2,357
  • 2
  • 18
  • 38