Reputation: 1840
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
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
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
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
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
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
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