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.
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.
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 .
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.
"isset() checks if a variable has a value including (False, 0 or empty string), but not NULL.
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.
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)) {
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.
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.
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;
}
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
Save yourself some typing and put it into a loop...
foreach (array('vFoo','vSomeValue','vAnother') as $varname) {
if (!empty($$varname)) $result .= '<li>'.$$varname.'</li>';
}
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 ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With