Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using if(!empty) with multiple variables not in an array

Tags:

php

is-empty

I am trying to polish some code with the if(!empty) function in PHP but I don't know how to apply this to multiple variables when they are not an array (as I had to do previously) so if I have:

$vFoo       = $item[1]; 
$vSomeValue = $item[2]; 
$vAnother   = $item[3];

Then I want to print the result only if there is a value. This works for one variable so you have:

 if (!empty($vFoo)) {
     $result .= "<li>$vFoo</li>";
 }

I tried something along the lines of

if(!empty($vFoo,$vSomeValue,$vAnother) {
    $result .= "<li>$vFoo</li>"
    $result .= "<li>$vSomeValue</li>"
    $result .= "<li>$vAnother</li>"
}

But of course, it doesn't work.

like image 832
HGB Avatar asked Feb 14 '11 14:02

HGB


People also ask

How do you check if two variables are empty in PHP?

When checking with the OR operator ( || ) the code will execute if one or none of them is empty. But you echo both variables even if one of them is empty. What I think you want to do is use the AND operator( && ). This way, the code will only execute if none of the variables are empty.

How do you check if two variables are NULL?

We used the && (and) operator to check if both variables are not equal to null . For the if block to run, both the conditions have to be satisfied. If the else block runs, at least one of the variables is equal to null .

Can I use empty instead of isset?

The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not. The isset() function will generate a warning or e-notice when the variable does not exists. The empty() function will not generate any warning or e-notice when the variable does not exists.

Does Isset check for empty string?

"isset() checks if a variable has a value including (False, 0 or empty string), but not NULL.


8 Answers

You could make a new wrapper function that accepts multiple arguments and passes each through empty(). It would work similar to isset(), returning true only if all arguments are empty, and returning false when it reaches the first non-empty argument. Here's what I came up with, and it worked in my tests.

function mempty()
{
    foreach(func_get_args() as $arg)
        if(empty($arg))
            continue;
        else
            return false;
    return true;
}

Side note: The leading "m" in "mempty" stands for "multiple". You can call it whatever you want, but that seemed like the shortest/easiest way to name it. Besides... it's fun to say. :)

Update 10/10/13: I should probably add that unlike empty() or isset(), this mempty() function will cry bloody murder if you pass it an undefined variable or a non-existent array index.

like image 195
imkingdavid Avatar answered Sep 21 '22 11:09

imkingdavid


You need to write a condition chain. Use && to test multiple variables, with each its own empty() test:

if (!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother)) {

But you probably want to split it up into three ifs, so you can apply the extra text individually:

if (!empty($vFoo)) {
   $result .= "<li>$vFoo</li>";
}
if (!empty($vSomeValue)) {
   $result .= "<li>$vSomeValue</li>";
}
if (!empty($vAnother)) {
like image 45
mario Avatar answered Sep 21 '22 11:09

mario


empty() can only accept one argument. isset(), on the other hand, can accept multiple; it will return true if and only if all of the arguments are set. However, that only checks if they're set, not if they're empty, so if you need to specifically rule out blank strings then you'll have to do what kelloti suggests.

like image 20
Andrew Avatar answered Sep 21 '22 11:09

Andrew


use boolean/logical operators:

if (!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother)) {
    $result .= "<li>$vFoo</li>"
    ...
}

Also, you might want to join these with or instead of and. As you can see, this can give you quite a bit of flexibility.

like image 24
kelloti Avatar answered Sep 20 '22 11:09

kelloti


I use this function as an alternative to isset. It's a little simpler than @imkingdavid's one and return true, if all arguments (variables) are not empty, or return false after first empty value.

function isNotEmpty() {
    foreach (func_get_args() as $val) {
        if (empty($val)) {
            return false;
        }
    }

    return true;
}
like image 33
hovado Avatar answered Sep 21 '22 11:09

hovado


Just adding to the post by imkingdavid;

To return TRUE if any of the parameters are empty, check out the below instead.

function mempty(...$arguments)
{
    foreach($arguments as $argument) {
        if(empty($argument)) {
            return true;
        }
    }
    return false;
}

This is closer to how isset() works, and is how i would expect empty() to work if supporting a variable length of parameters. This will also keep in line with how empty() works in that it will return FALSE if everything is not empty.


If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

PHP: isset - Manual

like image 22
Dylan Avatar answered Sep 20 '22 11:09

Dylan


Save yourself some typing and put it into a loop...

foreach (array('vFoo','vSomeValue','vAnother') as $varname) {
  if (!empty($$varname)) $result .= '<li>'.$$varname.'</li>';
}
like image 43
awm Avatar answered Sep 20 '22 11:09

awm


As others noted, empty() takes only one argument so you have to use something like

if(!empty($v1) && !(empty($v2) ...)

but if (!empty($v)) is the same thing as if($v) so you may also use:

if ($v1 && $v2 ...)
like image 40
Eelvex Avatar answered Sep 20 '22 11:09

Eelvex