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开发者_StackOverflow .= "<li>$vAnother</li>"
}
But of course, it doesn't work.
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 ...)
Why not just loop through the $item array
foreach($item as $v) {
if(!empty($v))) {
$result .= "<li>$v</li>";
}
}
You could also validate of the key index value as well
foreach($item as $key => $value) {
if($key == 'vFoo') {
if(!empty($value))) {
$result .= "<li>$value</li>";
}
}
// would add other keys to check here
}
For the empty() versus isset()
$zero = array(0, "0");
foreach($zero as $z) {
echo "\nempty(): $z ";
var_dump($z);
var_dump(empty($z)); // Will return true as in it's empty
echo "isset(): $z ";
var_dump(isset($z)); // will return true as it has value
}
Output:
empty(): 0 int(0)
bool(true)
isset(): 0 bool(true)
empty(): 0 string(1) "0"
bool(true)
isset(): 0 bool(true)
I'm not 100% sure I understand what you're trying to do, but here are a couple of possible answers:
This will return each variable only if it isn't empty:
function printIfNotEmpty($var) {
if (!empty($var)) {
return '<li>' . $var . '</var>';
}
return '';
}
$result .= printIfNotEmpty($vFoo);
$result .= printIfNotEmpty($vSomeValue);
$result .= printIfNotEmpty($vAnother);
Or this one will add all three of them if none of them are empty:
if (!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother)) {
$result .= '<li>' . $vFoo . '</li>';
$result .= '<li>' . $vSomeValue . '</li>';
$result .= '<li>' . $vAnother . '</li>';
}
Guys, many thanks for all your responses, I kind of created a script based on all of our possible answers which is actually helping me with learning PHP's syntax which is the cause behind most of my errors :) So here it is
$vNArray ['Brandon'] = $item[3];
$vNArray['Smith']= $item[4];
$vNArray ['Johnson']= $item[5];
$vNArray ['Murphy']= $item[6];
$vNArray ['Lepsky']= $item[7];
foreach ($vNArray as $key => $value){
if(!empty($value)){
$result .= "\t\t\t\t<li><strong>$key</strong>" .$value. "</li>\n";
}
I just need to figure out how to make some of these different now. So could I access each in the following way?
$result .= "\t\t\t\t<li><strong>$key[0]</strong>" .$value. "</li>\n";
$result .= "\t\t\t\t<li><a href="thislink.com">$key[3]</a>" .$value. "</li>\n";
Thanks guys!
Why not just like this:
foreach(array_filter($input_array) as $key => $value) {
// do something with $key and $value
}
Just for the record: array_reduce works as well.
if (!array_reduce($item, function($prev_result,$next) {
return $prev_result || empty($next); // $prev_result will be initial null=false
})) {
// do your stuff
}
If working only with strings, you may try the following:
if(empty($str1.$str2.$str3)){ /* ... */ }
Note, that empty(0) would return true, while empty('0'.'0') and empty('0') won't.
You can try php &&
/ and
operator. They both are same.
if(!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother) {
$result .= "<li>$vFoo</li>"
$result .= "<li>$vSomeValue</li>"
$result .= "<li>$vAnother</li>"
}
精彩评论